Pytest - run multiple tests from a single file
You don't have to run your tests manually. Pytest finds and executes them automatically:
# tests/test_positive.pydef test_pos_1(): populate_page() check_values()def test_pos_2(): process_entry() validate_result()
and
# tests/test_negative.pydef test_neg_1(): populate_page() validate_error()
If you then run
py.test
They should automatically be picked up.
You can then also select a single file
py.test -k tests/test_negative.py
or a single test
py.test -k test_neg_1
Start file names and tests with either test_
or end with _test.py
. Classes should begin with Test
Directory example:
|-- files
|--|-- stuff_in stuff
|--|--|-- blah.py
|--|-- example.py
|
|-- tests
|--|-- stuff_in stuff
|--|--|-- test_blah.py
|--|-- test_example.py
In terminal: $ py.test --cov=files/ tests/
or just $ py.test tests/
if you don't need code coverage. The test directory file path must be in your current directory path. Or an exact file path ofcourse
With the above terminal command ($ py.test tests/
), pytest will search the tests/ directory for all files beginning with test_
. The same goes for the file.
test_example.py
# imports heredef test_try_this(): assert 1 == 1# orclass TestThis: assert 1 == 0# or even:def what_test(): assert True