Pytest Cheat Sheet

A Programming › Python cheat sheet in English by nanditha

Pytest is one of the best tools used to boost software testing productivity.

29,053 views 3,719 downloads 7 Aug 2020 History

Basic

pytest test_m­od.py
Run tests in a module.
pytest testing/
Run tests in a directory.
pytest test_m­od.p­y:­:te­st_func
Run a specific test within a module.
pytest test_m­od.p­y:­:Te­stC­las­s::­tes­t_m­ethod
Run a specific method of a class.

General

-k "­exp­res­sio­n"
Run tests by keyword expres­sion.
-m "­exp­res­sio­n"
Run tests by marker expres­sion.
--fixtures
Shows builtin and custom fixtures.
--markers
Shows builtin and custom markers.
--lf, --last­-failed
Run only the failed tests from the previous run.
--ff, --fail­ed-­first
Rerun the failures first and then successful tests.
-x, --exit­first
Exit immedi­ately on first error or failed test.

Reporting

-v, --verbose
More verbosity.
-q, --quiet
Less verbosity.
--disa­ble­-wa­rnings,
--disa­ble­-py­tes­t-w­arnings
Disable the display of warnings summary.
--html­=path
Generate the HTML report at given path [pytes­t-h­tml].
-r
Display a short test summary info.
The -r option accepts a number of characters after it. Default is fE to list failures and errors.
f - failed
E - error
s - skipped
x - xfailed
X - xpassed
p - passed
P - passed with output
Special characters for (de)se­lection of groups:
a - all except pP
A - all
N - none, this can be used to display nothing (since fE is the default)

Debugging

-l,
--show­locals
Show local variable in traceback.
--full­-trace
Show complete traceb­acks. Default is to cut.
--pdb
Pytest places a debugger breakpoint whenever an error occurs in tests.
--co,
--collect-only
Collect tests, don't execute them.
--help
Lists all the Pytest and dependent packages command line options.

Parall­eli­zation [pytes­t-x­dist]

-n numpro­cesses
Number of processes to start. Can be a positive integer or Use '­aut­o' for auto detection CPUs number.
--dist no/ loadscope/ loadfile
Select the test distri­bution algorithm with the --dist option.
Distri­buting algorithm values are
no: --nump­roc­esses will send pending tests to any worker that is available, without any guaranteed order.
loadscope: Tests are grouped by the module for test functions and by class for test methods.
loadfile: Tests are grouped by their containing file.

Testrail [pytes­t-t­est­rail]

--testrail
Create and update testruns.
--tr-c­onf­ig=path
Defaults to testra­il.cfg .
--test­rai­l-r­un-name
Name given to Testrun.
pytest --testrail --tr-c­onfig= testra­il.cfg
Testrail config file template:
[API]
url = https:­//y­our­url.te­str­ail.net/
email = user@e­mai­l.com
password = <ap­i_k­ey>

[TESTRUN]
assign­edto_id = <us­er-­id>
project_id = <pr­oje­ct-­id>
suite_id = <te­st-­sui­te-­id>
name = <te­st-­run­-na­me>

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.