Reputation: 2489
I have a python file with some code and simple tests on that code.
I would like to invoke pytest from that python file, have it collect only the tests in that file, and run them.
For example, foo.py
:
# ... various code above...
def test_foo():
foo = Foo()
assert foo()
def test_bar():
bar = Bar()
assert bar.baz
if __name__ == '__main__':
import pytest
pytest.main()
I now would like to run python foo.py
and have pytest run the two tests in that file, and those tests only.
However, when I run python foo.py
, pytest collects ALL tests from all modules that are rooted from the path in which I ran the command.
How can I run python foo.py
and have the pytest.main()
invoked in foo.py
only call the tests in foo.py
and no others?
Upvotes: 6
Views: 2629
Reputation: 595
According to the pytest documentation, options and arguments can be passed to pytest.main
. To run tests in foo.py
, this would work:
# ... various code above...
def test_foo():
foo = Foo()
assert foo()
def test_bar():
bar = Bar()
assert bar.baz
if __name__ == '__main__':
import pytest
pytest.main(["foo.py"])
# consider using below
# pytest.main([__file__])
Upvotes: 6