Frequently Asked Question

Video: 6.3.4 Fixtures
Last Updated 3 years ago


Press "Ctrl + F" to find the keyword of your interest.

If you wish to have a direct link access to the video timestamps, please follow these instructions.

Found this video helpful? Why not take the whole HIL Specialist course? A Certificate is waiting for you for free at HIL Academy.

Would you or your organization benefit from having these videos narrated in your native language? Contact us and let us know if you wish to contribute.

TRANSCRIPT

00:00:02

Hello and welcome to our fourth lesson on Pie test. In this lesson, we will show what Pie  

00:00:07

test fixtures are and how to use them.A Pie test fixture is a special type of  

00:00:12

function that you can use in Pie test. It allows you to control procedures that  

00:00:16

should happen before and after tests and how often a fixture should be executed.  

00:00:20

A single fixture can be used by multiple tests.To better visualize how they work, we will be  

00:00:26

using print statements, both inside the tests and the fixtures. In this first example,  

00:00:31

let s add a message to be printed before the assert command of the test function. 

00:00:39

So, let s create a function called my fixture and add the Pie test fixture decorator. 

00:00:53

A fixture has three main stages: Setup, yield, and Teardown. Setup contains the procedures that  

00:01:00

should happen before the test. Yield gives control and sends information to the test.  

00:01:05

Teardown contains the procedures that happen after the test is done. 

00:01:09

Inside the my fixture function, let s print a message representing the setup procedure,  

00:01:14

yield control to the test without sending information, and print a message representing  

00:01:18

the teardown. Everything that comes before the yield is called setup and is going to  

00:01:29

run before the test, while everything that comes after the yield is called teardown. 

00:01:33

In order to link the test with the fixture,  

00:01:36

you only need to add the fixture name as an argument of the test function. 

00:01:43

Let s run the test, using Dash S and dash Q to display the printed  

00:01:47

statements and simplify the output text.After running the test, you can observe that  

00:01:53

the setup is executed just before the first test, indicated here by Test if one greater  

00:01:57

than 2 . The teardown is executed after it. The same happens for all the other tests. 

00:02:06

We can do a small change in the print function in order to have the assertion result just after  

00:02:11

the test print. That can be achieved by moving the break line to the beginning of the print.

00:02:30

We can also share this fixture with other tests. Let s copy and paste our test function and test if  

00:02:36

X is greater than 3. We can run Pie test, and we will have a similar result for both functions. 

00:02:52

Here you can observe that we have both tests running the  

00:02:55

fixture before and after each parameter.You might want to have the fixture running  

00:02:59

just once at the beginning and once at the end of the test. We can achieve that by changing  

00:03:04

the scope of the fixture to module .Now, you can observe that the fixture  

00:03:21

runs just once at the beginning and once at the end of the test. 

00:03:25

It is also possible to parametrize the fixtures and send the values through the yield.  

00:03:30

To do so, we can add the argument params to set the values. 

00:03:38

We also need to add the parameter request in the fixture so Pie test  

00:03:41

can include the values in the function.We can obtain the parameter value from  

00:03:46

request.param , so let s include it after the yield in order to pass this value to the test. 

00:03:52

This way, we can remove the parametrization from the test and include it in the fixture. 

00:04:07

After running the code, you can observe that the tests execution order changed. We run the  

00:04:12

fixture setup, parametrize the first value, run both tests, and finally the fixture teardown. 

00:04:20

We can also change our test structure so the setup and teardown are executed just once.  

00:04:25

We can achieve that by splitting our fixture into two. The first one can contain the setup  

00:04:30

and teardown procedure, while the second one parametrizes the tests. 

00:04:51

After these modifications, the tests will call my fixture which will call my main fixture . This  

00:04:56

shows that it is possible to link tests and fixtures in different ways. Let s run the code. 

00:05:09

Now the fixture is running once at the beginning and end of the test sequence. 

00:05:14

Let s say now we have a different test file called test_file2 . This file tests  

00:05:19

if a value is greater than four, using the same fixtures as before. 

00:05:29

Now, if we look at both test files, we can see the fixture repeated.  

00:05:34

To solve this issue, let s create a new file named conftest . This is a special file which  

00:05:39

Pie test will recognize and look for fixtures inside it. So, we can copy the fixtures and  

00:05:45

paste them into conftest to have them apply to all tests. Let s run the test again. 

00:06:05

Now we can see that the fixture runs once at the beginning and once at the end of  

00:06:08

the series of tests from each test file.If we would like the fixture to run once,  

00:06:13

for the whole test session, we need to change the fixture scope to session,  

00:06:18

inside conftest . Let s test it. 

00:06:27

Now the fixture was executed just once for the whole test session. 

00:06:31

Finally, you can also have fixtures in several  

00:06:34

conftests and test files . If the fixtures have the same name,  

00:06:38

Pie test will use the one that is saved closer to the test code or in parent folders. 

00:06:43

We can verify it by copying and pasting the fixture from the conftest to the test_file1 . Let  

00:06:49

s change the setup and tear down messages inside test_file1 . Let s run the test again.

00:07:09

We can see that since both fixtures have the session scope,  

00:07:12

the tear down of test_file1 happens only at the end of the test session. We can change that by  

00:07:17

changing the fixture scope to module , so the fixture tear down runs at the end of the module. 

00:07:33

As we can see, the execution order of the tests and fixture teardown have changed.  

00:07:38

Changing the scope of the fixture gives us flexibility to define how often and  

00:07:42

when each step happens, which may be useful depending on the application. 

00:07:46

In this lesson we learned how to write, execute, and parametrize functions with fixtures. Now  

00:07:53

that we ve learned the basics of Pie test, in the following lessons we will explore how to  

00:07:57

use it in the Typhoon Test I D E tool for running your HIL tests.

00:08:01

Thank you for watching!

Please Wait!

Please wait... it will take a second!