Sharing a Fixture Instance & Fixture Finalization

Python 3: Automating Your Job Tasks Superhero Level: Automate Unit Testing with Python 3
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.41
List Price:  €130.79
You save:  €37.37
£79.90
List Price:  £111.86
You save:  £31.96
CA$137.16
List Price:  CA$192.03
You save:  CA$54.87
A$153.08
List Price:  A$214.33
You save:  A$61.24
S$135.90
List Price:  S$190.27
You save:  S$54.36
HK$781.66
List Price:  HK$1,094.36
You save:  HK$312.69
CHF 91.23
List Price:  CHF 127.73
You save:  CHF 36.49
NOK kr1,107.76
List Price:  NOK kr1,550.92
You save:  NOK kr443.15
DKK kr696.86
List Price:  DKK kr975.63
You save:  DKK kr278.77
NZ$168.60
List Price:  NZ$236.05
You save:  NZ$67.44
د.إ367.24
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,972.60
List Price:  ৳15,362.09
You save:  ৳4,389.48
₹8,346.11
List Price:  ₹11,684.89
You save:  ₹3,338.77
RM475.54
List Price:  RM665.78
You save:  RM190.23
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,846.85
List Price:  ₨38,986.71
You save:  ₨11,139.85
฿3,694.58
List Price:  ฿5,172.56
You save:  ฿1,477.98
₺3,236.41
List Price:  ₺4,531.11
You save:  ₺1,294.69
B$519.35
List Price:  B$727.11
You save:  B$207.76
R1,861
List Price:  R2,605.48
You save:  R744.47
Лв182.73
List Price:  Лв255.83
You save:  Лв73.10
₩137,535.16
List Price:  ₩192,554.72
You save:  ₩55,019.56
₪374.71
List Price:  ₪524.61
You save:  ₪149.90
₱5,763.02
List Price:  ₱8,068.46
You save:  ₱2,305.43
¥15,525.12
List Price:  ¥21,735.79
You save:  ¥6,210.67
MX$1,695.96
List Price:  MX$2,374.42
You save:  MX$678.45
QR364.20
List Price:  QR509.89
You save:  QR145.69
P1,420.18
List Price:  P1,988.32
You save:  P568.13
KSh13,372.99
List Price:  KSh18,722.73
You save:  KSh5,349.73
E£4,798.26
List Price:  E£6,717.75
You save:  E£1,919.49
ብር5,719.92
List Price:  ብር8,008.12
You save:  ብር2,288.20
Kz83,641.63
List Price:  Kz117,101.63
You save:  Kz33,460
CLP$96,028.39
List Price:  CLP$134,443.59
You save:  CLP$38,415.20
CN¥723.89
List Price:  CN¥1,013.48
You save:  CN¥289.58
RD$5,819.58
List Price:  RD$8,147.64
You save:  RD$2,328.06
DA13,455.35
List Price:  DA18,838.03
You save:  DA5,382.68
FJ$226.17
List Price:  FJ$316.65
You save:  FJ$90.48
Q777.12
List Price:  Q1,088.01
You save:  Q310.88
GY$20,929.88
List Price:  GY$29,302.67
You save:  GY$8,372.79
ISK kr14,022.59
List Price:  ISK kr19,632.19
You save:  ISK kr5,609.60
DH1,011.06
List Price:  DH1,415.53
You save:  DH404.46
L1,768.33
List Price:  L2,475.73
You save:  L707.40
ден5,750.59
List Price:  ден8,051.06
You save:  ден2,300.46
MOP$805.69
List Price:  MOP$1,128
You save:  MOP$322.31
N$1,863.42
List Price:  N$2,608.86
You save:  N$745.44
C$3,672.13
List Price:  C$5,141.13
You save:  C$1,469
रु13,425.24
List Price:  रु18,795.88
You save:  रु5,370.63
S/376.35
List Price:  S/526.91
You save:  S/150.55
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,677.10
List Price:  ZK3,748.04
You save:  ZK1,070.94
L464.84
List Price:  L650.79
You save:  L185.95
Kč2,347.91
List Price:  Kč3,287.17
You save:  Kč939.26
Ft36,370.18
List Price:  Ft50,919.71
You save:  Ft14,549.52
SEK kr1,092.76
List Price:  SEK kr1,529.91
You save:  SEK kr437.15
ARS$87,614.14
List Price:  ARS$122,663.30
You save:  ARS$35,049.16
Bs690.85
List Price:  Bs967.22
You save:  Bs276.36
COP$390,329.27
List Price:  COP$546,476.60
You save:  COP$156,147.32
₡51,017.42
List Price:  ₡71,426.43
You save:  ₡20,409.01
L2,476.75
List Price:  L3,467.55
You save:  L990.79
₲749,236.02
List Price:  ₲1,048,960.40
You save:  ₲299,724.38
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł404.64
List Price:  zł566.52
You save:  zł161.87
Already have an account? Log In

Transcript

Following up on the previous lecture, what if you need to share a fixture, not between the test inside the same file where the fixture function is located. But instead you want to share the fixture across all tests in the sub directories have your root directory. For instance, I have created a new folder on my D drive called fixture sharing. We can call this the root directory. In this directory, I have what's called a contest.py file, and the subdirectory named test, which contains the test underscore results.py file holding the actual tests. You can download all of these files from the notebook following this video.

Now, keep in mind that in this type of setup, you can define your fixture inside the context.py file located in the root directory in order to have the fixture available across all sub directories. You Now let's open up the confidence.py file. And looking at the code inside this file, you can easily notice that it is the exact same fixture function that I used in the previous video. Now moving on to the subdirectory, let me open up the test results.py file as well. And notice that this file contains three tests that reference the XY func fixture function from within the context dot p y file. Okay, each of these tests is using a different number for the desired decimal places when applying the round function.

This is just to differentiate between the three tests. Now let's run these tests in the command line and see the results. So the CD fixture underscore sharing, Enter. And now I'm going to move over to the sub directory. So CD test and now let's run the test. So by Test, dash dash disable warning, and now the name of the file containing the test test underscore results dot p y enter.

As you can see, all three tests passed as expected. What is the advantage of sharing fixtures? Well, let's get back to the file. And let me add some more code and also uncomment. The print functions in each of these tests. Also, I'm going to add assert zero to the end of each test.

At this point, the assert zero statements will make sure that that each test fails because zero is always evaluated as Boolean false. Remember, we need our tests to fail just to see a detailed trace back and inspect how the fixture is used. Let's save the file as it is right now. And now let's run it once again inside the command line. Okay. Now, back to our question.

What is the benefit of fixture sharing? Well, if If you look carefully in this output, you will see the results of printing the memory location. So print of ID of x, y func. So you can see the results of printing the memory location of the x, y func object, the ID is the same for all the tests. And for the third test right here. This means that these three test functions run as fast as a single function, because they are reusing the same instance of x y func.

This can be really helpful, especially when the fixture function needs to connect to a remote server. For example, instead of creating a new connection, a new instance for each test, fixture sharing allows multiple tests to reuse the same instance. Let me undo my code changes. Okay, I'm going to save the file once again. Now let's get back to the test.py file for a bit. Notice something new here compared with previous vs video, the addition of the scope parameter to pi test dot fixture.

Now depending on your testing setup and needs, you can choose to share fixtures at five different levels, function class, module, package and session, and you can pick the desired level by passing it to the scope parameter. In my case, I want the fixture function to be available to test modules, such as my test underscore results.py file. So we want this fixture function to be available to all the test modules residing in any sub directories have the root directory, that's why I have provided the module string as a value for the scope parameter. Another thing that I want to mention here is that you can have constants.pi files containing fixture functions in your sub directories as well. Remember that fixture functions defined inside the root directory contest. UI file, as we have right here, these fixture functions are available to all the tests in all the sub directories.

Whilst fixture functions defined in a confidence.py file located in a sub directory, are available only for the test in that sub directory and its own sub directories, if any. Next let's talk a bit about fixture finalization as well. This refers to executing specific code when the fixture goes out of scope. So as soon as a module level fixture has been shared among all the tests in the module, and the last test in the module has finished its execution, we can use the yield statement. Actually, let me prove this to you. Let me show this to you.

In another folder that I have created picture underscore fin. So let's open up the confidence.py file from here and this is the yield statement I was talking about. So as soon as the last test in the module has finished execution, we can use the yield statement instead of the return statement, as we had here, in the previous example, the yield statement signaling that all the code following it should be executed serving as teardown code. Actually, let me explain this using an example. For instance, you are opening up a connection to a database server, and you read data from that database in order to run the tests inside the module. As soon as the last test finishes its execution.

Regardless of the test results, you can use the teardown code after the yield statement in your fixture function to properly close the connection to the database server. Now, if we look at this new folder right here, fixture underscore fin, it has the exact same structure as before the conference.py file and a subdirectory containing the tests. You can download the contents of this folder from the notebook, following the video. Now looking at the code from this new contest.py file, the difference lays in the fixture function. Previously, we had the return statement inside the fixture function. Here it is once again, with no other additional code.

This time we want to execute a piece of code as soon as the last test has finished its execution. This is why we need to replace return with the yield statement, followed by the desired tear down code. In my case, just a simple print function, the rest of the code inside context dot p y stays the same. The test functions stay the same, actually, let me prove this to you. So the test functions are exactly the same. Now let's run our tests to see if the teardown code gets executed or not.

Keep in mind that you need to use the dash s option as well in order to see the results of running the teardown code. So I'm going to move over To the other folder, that would be CD fixture, Finn CD test. And finally, we have pi test, dash s, also dash dash, disable warning, and finally, the name of the file test results.pi. And indeed, there it is. There's the code being executed the code following the yield statement. Now the last thing I want to mention in this video is that you can use another way of inserting teardown code without using the yield statement.

If you look into my test documentation, you will find out that you can also use what's called a finalizer function. also keeping the old return statement, the finalizer function has to be defined inside the fixture function. So there it is right here, right before the return statement. The role of the finalizar function is the same executing teardown code after the last step. finishes its execution. As a small homework, check out the link attached to this lecture and research the implementation and advantages of the finalizer function.

Then check out the code in the notebook following this lecture and I will see you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.