Run multiple tests , specific test and Run a subset of entire test
Run multiple tests and
specific test
Suppose we have multiple files , say test_addition.py
, test_subtraction.py. To run all the tests from all the files in the folder
and subfolders we need to just run the pytest command.
>>> pytest
This will run all the filenames starting with test_
and the filenames ending with _test in that folder and subfolders under that
folder.
To run tests only from a specific file, we can use
pytest <filename>
Ex: pytest test_addition.py
Output will be
Run a subset of
entire test
we will have multiple test files and each file will
have a number of tests. Tests will cover various modules and functionalities. How
to run only a specific set of tests?
Pytest provides two ways to run the subset of the
test suite.
To run based on substring matching of test names.
To run based on the markers applied.
Create two test files as test_addition.py and
test_subtraction.py
Below is the code for test_addition.py
import pytest
def
test_add():
assert (10+5 ==15)
def
test_add_even():
assert(10+10==20)
def
test_add_odd():
assert(5+7==13)
Below is the code for test_subtraction.py
import pytest
def
test_subtract():
assert (10-5==6)
def
test_subtract_even():
assert(10-4==6)
def
test_subtract_odd():
assert (7-3==4)
Option 1: To run based on substring matching of
test names.
To run all the tests having odd in its name we have to run.
Syntax: pytest -k <substring> -v
-k <substring> represents the substring to search for in the
test names.
Ex: pytest -k odd -v
Output will be
This will execute all the test names having the word ‘odd in
its name. In this case, they are test_add_odd() and test_subtract_odd(). In
the result, we can see 4 tests deselected. This is because those test names do
not contain the word odd in them.
Option 2: To run based on the markers applied.
Pytest allows us to set various attributes for the
test methods using pytest markers. To use markers in the test file, we need to
import pytest on the test files. We can define our own marker names to the
tests and run the tests having those marker names.
To run the marked tests, we can use the following
syntax
Pytest -m <markerName> -v
Create two test files as test_addition.py and
test_subtraction.py
Below is the code for test_addition.py
import pytest
@pytest.mark.add
def test_add():
assert (10+5 ==15)
@pytest.mark.even
def test_add_even():
assert(10+10==20)
@pytest.mark.odd
def test_add_odd():
assert(5+7==13)
@pytest.mark.add
def test_add():
assert (10+5 ==15)
@pytest.mark.even
def test_add_even():
assert(10+10==20)
@pytest.mark.odd
def test_add_odd():
assert(5+7==13)
Below is the code for test_subtraction.py
import pytest
@pytest.mark.sub
def test_subtract():
assert (10-5==6)
@pytest.mark.even
def test_subtract_even():
assert(10-4==6)
@pytest.mark.odd
def test_subtract_odd():
assert (7-3==4)
@pytest.mark.sub
def test_subtract():
assert (10-5==6)
@pytest.mark.even
def test_subtract_even():
assert(10-4==6)
@pytest.mark.odd
def test_subtract_odd():
assert (7-3==4)
We can run the marked test by
Pytest -m odd -v
Output will be
It ran two tests marked as odd
Comments
Post a Comment