Listing 5-1. test_module01.py
def test_case01():
assert 'python'.upper() == 'PYTHON'
In Listing
5-1
, we are importing pytest in the first line. test_case01() is the test
function. Recall that assert is a Python built-in keyword. Also, just like with nose, we do
not need to extend these tests from any class. This helps keep the code uncluttered.
Run the test module with the following command:
python3 -m pytest test_module01.py
The output is as follows:
============================ test session starts ====================
platform linux -- Python 3.4.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0
rootdir: /home/pi/book/code/chapter05/test, inifile:
collected 1 items
test_module01.py .
========================== 1 passed in 0.05 seconds =================
You can also use verbose mode:
python3 -m pytest -v test_module01.py
The output is as follows:
========================= test session starts ===========================
platform linux -- Python 3.4.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0 --
/usr/bin/python3
cachedir: .cache
rootdir: /home/pi/book/code/chapter05/test, inifile:
collected 1 items
test_module01.py::test_case01 PASSED
======================== 1 passed in 0.04 seconds ====================
www.allitebooks.com
Chapter 5
■
pytest
89
Running Tests with the py.test Command
You can also run these tests with pytest’s own command, called py.test:
py.test test_module01.py
The output is as follows:
======================= test session starts =========================
platform linux -- Python 3.4.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0
rootdir: /home/pi/book/code/chapter05/test, inifile:
collected 1 items
test_module01.py .
======================= 1 passed in 0.04 seconds =====================
You can also use verbose mode as follows:
py.test test_module01.py -v
The output in the verbose mode is as follows:
============================ test session starts =======================
platform linux -- Python 3.4.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0 --
/usr/bin/python3
cachedir: .cache
rootdir: /home/pi/book/code/chapter05/test, inifile:
collected 1 items
test_module01.py::test_case01 PASSED
============================ 1 passed in 0.04 seconds =================
For the sake of simplicity and convenience, from now onward, we will use the same
method to run these tests for rest of the chapter and book. We will use pytest in the next
chapter to implement test-driven development. Also, observe when you run your own
tests that the output of test execution is in color by default, although the book shows the
results in black and white. You do not have to use any external or third-party plugin for
this effect. Figure
5-1
shows a screenshot of an execution sample.
Chapter 5
■
pytest
90
Test Class and Test Package in pytest
Like all the previous test automation frameworks, in pytest you can create test classes
and test packages. Take a look at the code in Listing
5-2
as an example.
Do'stlaringiz bilan baham: |