How to Unit-Tests with Pytest

draft

Start using pytest, a testing framework to write various tpyes of software tests. Possible tests: unit tests, integration tests, end-to-end tests and functional tests. It includes parametrized testing, fixtures and assert re-writing.

wiki pytest

pytest.org

pytest.org index

Testing through the Python interpreter is almost equivalent, except it will add the current directory to sys.path

python -m pytest [...]

TOCHECK Calling pytest from Python code You can invoke pytest from Python code directly:

retcode = pytest.main()

this acts as if you would call “pytest” from the command line. It will not raise SystemExit but return the exit code instead.




pytest support tests in tree or out of tree ..

how organize your tests




Pytest is quiet so use “-v”

$ pytest -v 

to print print statements in prog_test.py or prog.py file use “pytest -s”

@pytest.mark.parametrize(
('input_n', 'expected'),
(
(7, 49),
(8, 64)
)
)

def test_square2(input_n, expected):
print(f'{input_n=}')        # print output in pytest file
assert t.square(input_n) == expected

$ pytest -s 




YOUTUBE: write pytest for argparser




pytest for python cli (argparser)

capsys (capture system output)

def test_main(capsys):




Troubleshooting Pytest Error:

E ModuleNotFoundError: No module named 

SOLUTION (init.py file in Test Directory with)

import sys
sys.path.append('.')
@huichen __init__.py tells Python that the folder is a module. Without it, the folder is not a module and so Python
cannot find its name when used in import statements. –
theberzi
Commented Sep 6, 2022 at 7:35

Solution 1: PYTHONPATH env. var

Crate fiel “init.py”:

# better

import sys
sys.path.append('.')




Solution 2: use the PYTHONPATH env. var (see on stackoverflow pythonpath)

PYTHONPATH=. pytest

As mentioned by @J_H, you need to explicitly add the root directory of your project, since pytest only adds to sys.path directories where test files are (which is why @Mak2006’s answer worked.)

Good practice: use a Makefile or some other automation tool

If you do not want to type that long command all the time, one option is to create a Makefile in your project’s root dir with, e.g., the following:

.PHONY: test
test:
PYTHONPATH=. pytest

Which allows you to simply run:

make test




Pytest Links from antonywritescode

Youtube: search pytest anthonywritescode

Youtube: getting started with pytest (beginner - intermediate) anthony explains #518

Test Driven Development

python: raise SystemExit (beginner - intermediate) anthony explains #331

pytest: testing env variables (intermediate) anthony explains #317

pytest: xfail vs xpass and all test statuses (beginner - intermediate) anthony explains #260

replay - pytest, github actions, and advent of code - replay 2020-05-23

replay - pytest-nunit + iptables - 2020-08-03

python cli tested with pytest - (beginner to intermediate) anthony explains #001

pytest: parametrize permutation (intermediate) anthony explains #174

pytest’s parametrize (beginner - intermediate) anthony explains #027

pytest: everything you need to know about fixtures (intermediate) anthony explains #487

pytest: parametrize permutation (intermediate) anthony explains #174

pytest: testing exceptions (beginner - intermediate) anthony explains #176

are your python tests even running? (intermediate) anthony explains #438

why pytest.mark.usefixtures? (intermediate) anthony explains #098

replay - pytest capfd fixing - 2021-04-26

pytest: everything you need to know about fixtures (intermediate) anthony explains #487

testing output with pytest (beginner - intermediate) anthony explains #246

I don’t use pytest-cov (intermediate) anthony explains #489




Pytest Links from Pycharm

How to write unit tests in Python using pytest and PyCharm




Pytest links from others

pytest Basics: Introduction to Tests

Pytest Tutorial – How to Test Python Code (freeCodeCamp.org)




TOX (anthonywritescode)

Command line driven CI frontend and development task automation tool (https://github.com/tox-dev/tox)

Tox aims to automate and standardize testing in Python. It is part of a larger vision of easing the packaging, testing and release process of Python software (alongside pytest and devpi).

Links:

https://tox.wiki/en/latest/user_guide.html#basic-example

https://www.youtube.com/watch?v=SFqna5ilqig




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Android Analyse
  • Sreaming Atemmini
  • Android Aleap
  • GitHub Copilot
  • Android Security