The link to the original source code is given on the official book's side
https://pragprog.com/titles/bopytest2/python-testing-with-pytest-second-edition/
Create a virtual environment, activate it on POSIX systems, and install pytest
$ python3 -m venv my_venv
$ source my_venv/bin/activate
(my_venv) $ pip install pytest
or using virtualenv
$ python3 -m pip install virtualenv
$ python3 -m virtualenv my_venv
$ source my_venv/bin/activate
(my_venv) $ pip install pytest
Deactivate the venv
(my_venv) $ deactivate
Create a virtual environment, activate it on Windows systems, and install pytest
C:\> python -m venv my_venv
C:\> my_venv\Scripts\activate.bat
(my_venv) C:\> pip install pytest
Activate in PowerShell
C:>my_venv\Scripts\Activate.ps1
Run a test modul:
$ pytest 01-introduction/test_01-1--passing.py
Run a test modul with the --verbose
or -v
flag:
$ pytest -v 01-introduction/test_01-1--passing.py
Run all tests starting with test_
or ending with _test
in the current working directory without traceback
$ pytest --tb=no
Run tests given by their names or (sub)directories in which they are located
$ pytest --tb=no 01-introduction/test_01-1--passing.py 01-introduction/test_01-2--failing.py
$ pytest --tb=no 01-introduction
Run only specified functions
$ pytest -v 01-introduction/test_01-1--passing.py::test_passing
test_<something>.py
or<something>_test.py
for filestest_<something>
for methods and functionsTest<Something>
for classes