18 Pytest
18 Pytest
Pytest
Table of Contents
Advantages of Pytest..............................................................................................................2
How to define pytest test?......................................................................................................2
How to run all test cases in two different class in Pytest..........................................................2
Why we use -v on the console?................................................................................................2
How to run tests from a specific file?.......................................................................................2
What is -k on console?.............................................................................................................3
How to do group testing in Pytest?..........................................................................................3
What is fixture?.......................................................................................................................4
What is limitation of fixture?...................................................................................................5
What is conftest.py file?..........................................................................................................5
Parameterize testing in PyTest?..............................................................................................5
Why do xfail and skip and skipif use in PyTest?.......................................................................5
What is maxfail and why is it used?........................................................................................6
PyTest Parallel execution........................................................................................................6
Xml file in PyTest.....................................................................................................................7
What is –collect-only option?..................................................................................................7
What are-x, –exitfirst?.............................................................................................................7
What are -s and –capture=method?........................................................................................7
What are -q, –quiet?...............................................................................................................8
–durations=N..........................................................................................................................8
Assert in Pytest.......................................................................................................................8
Usefixtures..............................................................................................................................8
Using monkeypatch.................................................................................................................9
Pytest picked...........................................................................................................................9
Advantages of Pytest
The advantages of Pytest are as follows
Pytest can run multiple tests in parallel, which reduces the execution time of the test
suite.
Pytest has its own way to detect the test file and test functions automatically, if not
mentioned explicitly.
Pytest allows us to skip a subset of the tests during execution.
Pytest allows us to run a subset of the entire test suite.
Pytest is free and open source.
Because of its simple syntax, pytest is very easy to start with.
What is (.) dot, F and E on the console when you execute pytest?
(.) means pass, F means fail, E mean exception
pytest -v
What is -k on console?
To execute the tests containing a string in its name we can use the following syntax :
-k <substring> represents the substring to search for in the test names. -k EXPRESSION only run
tests/classes which match the given substring expression.
To use markers, we have to import pytest module in the test file. We can define our own
marker names to the tests and run the tests having those marker names.
A test function can use a fixture by mentioning the fixture name as an input parameter.
Here, we have a fixture function named input_value, which supplies the input to the tests. To
access the fixture function, the tests have to mention the fixture name as input parameter.
Pytest while the test is getting executed, will see the fixture name as input parameter. It then
executes the fixture function and the returned value is stored to the input parameter, which
can be used by the test.
Result
What is limitation of fixture?
However, the approach comes with its own limitation. A fixture function defined inside a test
file has a scope within the test file only. We cannot use that fixture in another test file. To make
a fixture available to multiple test files, we have to define the fixture function in a file called
conftest.py. conftest.py
In this case we are getting five tests: for number 1, 2, 3, 0 and 42. Each of those tests can fail
independently of one another (if in this example the test with 0 will fail, and four others will
pass). This approach is much more convenient for debugging and development compared with
a simple loop with an assert in it. The simple loop will not be able to run a test for 42 after the
test for 0 fails, but parametrization allows us to see results for all cases, even if they happen
after the failed test case.
In these situations, we have the option to xfail the test or skip the tests.
Pytest will execute the xfailed test, but it will not be considered as part failed or passed tests.
Details of these tests will not be printed even if the test fails (remember pytest usually prints
the failed test details). We can xfail tests using the following marker –
Skipping a test means that the test will not be executed. We can skip tests using the following
marker –
Skipif: If you wish to skip something conditionally then you can use skipif instead.
The code is qualified for deploying to production only if the test suite passes. If there is test
failure, whether it is one or many, the code is not production ready.
Therefore, what if we want to stop the execution of test suite soon after n number of test fails.
This can be done in pytest using maxfail.
The syntax to stop the execution of test suite soon after n number of test fails is as follows –
Here, we are going to stop the execution of the test after one failure itself by −
We will not be having much time difference when there is only a few tests to run. However, it
matters when the test suite is large.
We will now execute the tests from test_multiplcation.py and generate the xml by running
In this file, you can see testsuites which shows how many test executed and how many tests are
failures. Testcase gives the details of each executed test.
–durations=N
The --durations=N option is incredibly helpful when you’re trying to speed up your test suite. It
doesn’t change how your tests are run; it reports the slowest N number of
tests/setups/teardowns after the tests run. If you pass in --durations=0, it reports everything in
order of slowest to fastest.
Assert in Pytest
When you write test functions, the normal Python assert statement is your primary tool to
communicate test failure. The simplicity of this within pytest is brilliant. It’s what drives a lot of
developers to use pytest over other frameworks.
If you’ve used any other testing framework, you’ve probably seen various assert helper
functions. For example, the following is a list of a few of the assert forms and assert helper
functions:
Usefixtures
You can also mark a test or a class with @pytest.mark.usefixtures(’fixture1’, ’fixture2’).
usefixtures takes a string that is composed of a comma-separated list of fixtures to use. It
doesn’t make sense to do this with test functions—it’s just more typing. But it does work well
for test classes. Using usefixtures is almost the same as specifying the fixture name in the test
method parameter list. The one difference is that the test can use the return value of a fixture
only if it’s specified in the parameter list. A test using a fixture due to usefixtures cannot use the
fixture’s return value.
Pytest picked
Pytest picked is a package and you need install using pip. It provides to run the tests related to
the unstaged files or the current branch (according to Git)