Pytest - run multiple tests from a single file
Solution 1
You don't have to run your tests manually. Pytest finds and executes them automatically:
# tests/test_positive.py
def test_pos_1():
populate_page()
check_values()
def test_pos_2():
process_entry()
validate_result()
and
# tests/test_negative.py
def 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
Solution 2
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 here
def test_try_this():
assert 1 == 1
# or
class TestThis:
assert 1 == 0
# or even:
def what_test():
assert True
Solution 3
You need to change the class name
class Positive_tests: to--> class Test_Positive:
The class name should also start with "Test" in order to be discovered by Pytest. If you don't want to use "Test" in front of your class name you can configure that too, official doc here
ChrisG29
Updated on June 12, 2022Comments
-
ChrisG29 almost 2 years
I'm using Pytest (Selenium) to execute my functional tests. I have the tests split across 2 files in the following structure:
My_Positive_Tests.py
class Positive_tests: def test_pos_1(): populate_page() check_values() def test_pos_2(): process_entry() validate_result()
My_Negative_Tests.py
class Negative_tests: def test_neg_1 populate_page() validate_error()
The assertions are done within the functions (check_values, validate_result, validate_error). I'm trying to find a way to run all the tests from a single file, so that the main test file would look something like:
My_Test_Suite.py
test_pos_1() test_pos_2() test_neg_1()
Then from the command line I would execute:
py.test --tb=short "C:\PycharmProjects\My_Project\MyTest_Suite.py" --html=report.html
Is it possible to do something like this? I've been searching and haven't been able to find out how to put the calls to those tests in a single file.
-
SkylerHill-Sky almost 8 yearscould you update this post to accept an answer? or do you still have questions? @Chris
-
-
ChrisG29 almost 8 yearsI tried running py.test from the directory that the tests are in, but then the output was "no tests ran in 0.03 seconds"
-
Nils Werner almost 8 yearsDo you have the
test_
prefix on both files and functions? -
SkylerHill-Sky almost 8 yearsYes. If you're confused, I explained it with as much detail as I could
-
Nils Werner almost 8 yearsWell, did you try what I wrote? Remove all classes etc. Because that is how pytest is supposed to work. Whatever you are trying is against pytest's design.
-
ChrisG29 almost 8 yearsThank you for all the help. I didn't have the tests prefixed with "test_". Once I did that and removed the classes it worked perfectly.
-
Kedar.Aitawdekar about 5 yearswhat if I wanted to have sequenced run for the test? e.g: wanted to execute file: test_one.py then test_two.py ?
-
Nils Werner about 5 years@Kedar.Aitawdekar please ask this in a new Question