[go: up one dir, main page]

0% found this document useful (0 votes)
368 views9 pages

18 Pytest

The document provides information about various features of Pytest, a Python testing framework. It discusses advantages of Pytest like parallel test execution and automatic test detection. It also covers topics like defining and running tests, using markers to group tests, fixtures to share setup code between tests, parameterizing tests, skipping or expected failures of tests, and running tests in parallel.

Uploaded by

xavi life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
368 views9 pages

18 Pytest

The document provides information about various features of Pytest, a Python testing framework. It discusses advantages of Pytest like parallel test execution and automatic test detection. It also covers topics like defining and running tests, using markers to group tests, fixtures to share setup code between tests, parameterizing tests, skipping or expected failures of tests, and running tests in parallel.

Uploaded by

xavi life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Projects Scrum Java Verbal Java Code Selenium Verbal SQL

Selenium Code Cucumber TestNG Rest Assured SOAP


Postman JMeter Maven Jenkins GitHub
Mobile Testing Python Verbal PyTest Python Code PyTest Code

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.

How to define pytest test?


Pytest requires the test function names to start with test. Function names which are not of
format test* are not considered as test functions by pytest. We cannot explicitly make pytest
consider any function not starting with test as a test function.

What is (.) dot, F and E on the console when you execute pytest?
(.) means pass, F means fail, E mean exception

How to run all test cases in two different class in Pytest


For instance, you have two different files like first.py and second.py. To execute all test cases
from two different file, type on the concole

pytest -v

Why we use -v on the console?


V means verbosity. You will have more details.
How to run tests from a specific file?
pytest first.py -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.

Result: Including method names

How to do group testing in Pytest?


Pytest allows us to use markers on test functions. Markers are used to set various
features/attributes to test functions. Pytest provides many inbuilt markers such as xfail, skip
and parametrize. Apart from that, users can create their own marker names. Markers are
applied on the tests using the syntax given below:

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.

To run the marked tests, we can use the following syntax:

-m <markername> represents the marker name of the tests to be executed.


What is fixture?
Fixtures are functions, which will run before each test function to which it is applied. Fixtures
are used to feed some data to the tests such as database connections, URLs to test and some
sort of input data. Therefore, instead of running the same code for every test, we can attach
fixture function to the tests and it will run and return the data to the test before executing each
test.
A function is marked as a fixture by –

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.

Execute the test using the following command –

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

What is conftest.py file?


This file provides accessible across multiple test files. We can define the fixture functions in this
file to make them accessible across multiple test files. The tests will look for fixture in the same
file. As the fixture is not found in the file, it will check for fixture in conftest.py file. On finding it,
the fixture method is invoked, and the result is returned to the input argument of the test.

Parameterize testing in PyTest?


Parameterizing of a test is done to run the test against multiple sets of inputs. We can do this
by using the following marker-

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.

Why do xfail and skip and skipif use in PyTest?


Some test cases are not ready to test sometimes. For instance, a test is not relevant for some
time due to some reasons. A new feature is being implemented and we already added a test for
that feature.

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.

What is maxfail and why is it used?


In a real scenario, once a new version of the code is ready to deploy, it is first deployed into pre-
prod/staging environment. Then a test suite runs on it.

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 −

PyTest Parallel execution


By default, pytest runs tests in sequential order. In a real scenario, a test suite will have a
number of test files and each file will have a bunch of tests. This will lead to a large execution
time. To overcome this, pytest provides us with an option to run tests in parallel.

For this, we need to first install the pytest-xdist plugin.


Install pytest-xdist by running the following command –

we can run tests by using the syntax pytest -n <num>

-n <num> runs the tests by using multiple workers, here it is 3.

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.

Xml file in PyTest


We can generate the details of the test execution in an xml file. This xml file is mainly useful in
cases where we have a dashboard that projects the test results. In such cases, the xml can be
parsed to get the details of the execution.

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.

What is –collect-only option?


The --collect-only option shows you which tests will be run with the given options and
configuration. It’s convenient to show this option first so that the output can be used as a
reference for the rest of the examples.

What are -x, –exitfirst?


Normal pytest behavior is to run every test it finds. If a test function encounters a failing assert
or an exception, the execution for that test stops there and the test fails. And then pytest runs
the next test. Most of the time, this is what you want. However, especially when debugging a
problem, stopping the entire test session immediately when a test fails is the right thin

What are -s and –capture=method?


The -s flag allows print statements—or really any output that normally would be printed to
stdout —to actually be printed to stdout while the tests are running. It is a shortcut for
--capture=no. This makes sense once you understand that normally the output is captured on
all tests. Failing tests will have the output reported after the test runs on the assumption that
the output will help you understand what went wrong. The -s or --capture=no option turns off
output capture. When developing tests, I find it useful to add several print() statements so that
I can watch the flow of the test.

What are -q, –quiet?


The -q/--quiet option is the opposite of -v/--verbose; it decreases the information reported.

–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.

Using monkey patch


A “monkey patch” is a dynamic modification of a class or module during runtime. During
testing, “monkey patching” is a convenient way to take over part of the runtime environment of
the code under test and replace either input dependencies or output dependencies with
objects or functions that are more convenient for testing. The monkey patch built-in fixture
allows you to do this in the context of a single test. And when the test ends, regardless of pass
or fail, the original unpatched is restored, undoing everything changed by the patch.
The monkey patch fixture helps you to safely set/delete an attribute, dictionary item or
environment variable, or to modify sys.path for importing.

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)

You might also like