pytest - adeemacdowell/adeemacdowell.github.io GitHub Wiki

Setup

Pycharm

Settings>Lang and framework>Django>don't use django test runner == True

Tools>Python integrated tools>default test runner>pytest

Debugging

Open the test.py

Alt>R then "pytest in xxx.py"

Others

Adam Parkin - https://www.youtube.com/channel/UCRjnjPqSWVn2e0EQxQlZEzw/videos

pytest coverage

pytest -s -q -x --failed-first

py.test --cov=knaps.stock

py.test --cov=knaps.purchasing.sales.views

pytest -q C:\Dev\knaps\knaps\purchasing\sales\tests\views --cov=knaps.purchasing.sales.views

pytest -q C:\Dev\knaps\knaps\purchasing\sales\tests\views --cov=knaps.purchasing.sales.views --cov-report=html

--durations=3 # gives you the 3 slowest tests

pytest -k "notes" # filters all tests by "notes"

-s Show Output, do not caputure

-q, --quiet Less verbose

-x Stop after first failure

pytest -x # stop after first failure

--ff,--failed-first- to run the failures first and then the rest of the tests.

approx

from pytest import approx

0.1 + 0.2 == approx(0.3)

True

video notes

vrc.py

records real responses from web apis to reuse them in tests

@pytest.mark.vcr

Others

--ds=config.settings.test_with_mysql

https://us02web.zoom.us/rec/play/upQpfur5rjo3EtDH4QSDUfd6W42-eK-s2nQX__MJmk-zUXdWZFryZeFAM-ES5xLUwIMzeEBwGu7PQ2hi

flakey tests https://testandcode.com/50

https://youtu.be/etosV2IWBF0

From the pytest docs

Test javascript using pytest as a test driver

https://pypi.org/project/oejskit/

https://www.youtube.com/watch?time_continue=2&v=7it7JFPInX0&feature=emb_logo

----------pytest-----------------

Run a single test [autokey=markslow]

@pytest.mark.slow

pytest -q -x path.py -m slow

--

To get a list of the slowest 10 test durations:

pytest --durations=10

--

To create plain-text machine-readable result files you can issue:

pytest --resultlog=path

--

@pytest.mark.skip

@pytest.mark.xfail

--disable-warnings

-v, --verbose increase verbosity.

-q, --quiet decrease verbosity.

--color=color color terminal output (yes/no/auto).

-x, --exitfirst exit instantly on first error or failed test.

--pdb start the interactive Python debugger on errors or KeyboardInterrupt.

--nf, --new-first run tests from new files first, then the rest of the tests sorted by file mtime

--lf last failed

----------mysql tests-----------------

pytest -s -q -x --failed-first --ds=config.settings.test_with_mysql

Others

pytest -q --failed-first

pytest -s -q -x --failed-first

pytest -s -q -x --failed-first --ds=config.settings.test_with_mysql

pytest -s -q -x --failed-first C:\Dev\knaps\knaps\purchasing\tillbalance\tests\views\test_update_payment_details.py