Frequently Asked Question
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
Hello and welcome to our fourth lesson on Pie test. In this lesson, we will show what Pie
test fixtures are and how to use them.A Pie test fixture is a special type of
function that you can use in Pie test. It allows you to control procedures that
should happen before and after tests and how often a fixture should be executed.
A single fixture can be used by multiple tests.To better visualize how they work, we will be
using print statements, both inside the tests and the fixtures. In this first example,
let s add a message to be printed before the assert command of the test function.
So, let s create a function called my fixture and add the Pie test fixture decorator.
A fixture has three main stages: Setup, yield, and Teardown. Setup contains the procedures that
should happen before the test. Yield gives control and sends information to the test.
Teardown contains the procedures that happen after the test is done.
Inside the my fixture function, let s print a message representing the setup procedure,
yield control to the test without sending information, and print a message representing
the teardown. Everything that comes before the yield is called setup and is going to
run before the test, while everything that comes after the yield is called teardown.
In order to link the test with the fixture,
you only need to add the fixture name as an argument of the test function.
Let s run the test, using Dash S and dash Q to display the printed
statements and simplify the output text.After running the test, you can observe that
the setup is executed just before the first test, indicated here by Test if one greater
than 2 . The teardown is executed after it. The same happens for all the other tests.
We can do a small change in the print function in order to have the assertion result just after
the test print. That can be achieved by moving the break line to the beginning of the print.
We can also share this fixture with other tests. Let s copy and paste our test function and test if
X is greater than 3. We can run Pie test, and we will have a similar result for both functions.
Here you can observe that we have both tests running the
fixture before and after each parameter.You might want to have the fixture running
just once at the beginning and once at the end of the test. We can achieve that by changing
the scope of the fixture to module .Now, you can observe that the fixture
runs just once at the beginning and once at the end of the test.
It is also possible to parametrize the fixtures and send the values through the yield.
To do so, we can add the argument params to set the values.
We also need to add the parameter request in the fixture so Pie test
can include the values in the function.We can obtain the parameter value from
request.param , so let s include it after the yield in order to pass this value to the test.
This way, we can remove the parametrization from the test and include it in the fixture.
After running the code, you can observe that the tests execution order changed. We run the
fixture setup, parametrize the first value, run both tests, and finally the fixture teardown.
We can also change our test structure so the setup and teardown are executed just once.
We can achieve that by splitting our fixture into two. The first one can contain the setup
and teardown procedure, while the second one parametrizes the tests.
After these modifications, the tests will call my fixture which will call my main fixture . This
shows that it is possible to link tests and fixtures in different ways. Let s run the code.
Now the fixture is running once at the beginning and end of the test sequence.
Let s say now we have a different test file called test_file2 . This file tests
if a value is greater than four, using the same fixtures as before.
Now, if we look at both test files, we can see the fixture repeated.
To solve this issue, let s create a new file named conftest . This is a special file which
Pie test will recognize and look for fixtures inside it. So, we can copy the fixtures and
paste them into conftest to have them apply to all tests. Let s run the test again.
Now we can see that the fixture runs once at the beginning and once at the end of
the series of tests from each test file.If we would like the fixture to run once,
for the whole test session, we need to change the fixture scope to session,
inside conftest . Let s test it.
Now the fixture was executed just once for the whole test session.
Finally, you can also have fixtures in several
conftests and test files . If the fixtures have the same name,
Pie test will use the one that is saved closer to the test code or in parent folders.
We can verify it by copying and pasting the fixture from the conftest to the test_file1 . Let
s change the setup and tear down messages inside test_file1 . Let s run the test again.
We can see that since both fixtures have the session scope,
the tear down of test_file1 happens only at the end of the test session. We can change that by
changing the fixture scope to module , so the fixture tear down runs at the end of the module.
As we can see, the execution order of the tests and fixture teardown have changed.
Changing the scope of the fixture gives us flexibility to define how often and
when each step happens, which may be useful depending on the application.
In this lesson we learned how to write, execute, and parametrize functions with fixtures. Now
that we ve learned the basics of Pie test, in the following lessons we will explore how to
use it in the Typhoon Test I D E tool for running your HIL tests.
Thank you for watching!