Reputation: 10812
using
This is my test case under tests/test_8_2_openpyxl.py
class TestSomething(unittest.TestCase):
def setUp(self):
# do setup stuff here
def tearDown(self):
# do teardown stuff here
def test_case_1(self):
# test case here...
I use unittest
style to write my test case. I use pytest to run the tests.
I have also setup and teardown functions following unittest
conventions
My commandline to run the tests become
pytest -s -v tests/test_8_2_openpyxl.py
It works as expected
When I debug sometimes, i want to be able to easily turn off either setup or teardown or both at the same time using some kind of commandline option
pytest -s -v tests/test_8_2_openpyxl.py --skip-updown
in order to skip both teardown and setup
pytest -s -v tests/test_8_2_openpyxl.py --skip-setup
in order to skip setup
pytest -s -v tests/test_8_2_openpyxl.py --skip-teardown
in order to skip teardown
sys.argv
I have tried using sys.argv
class TestSomething(unittest.TestCase):
def setUp(self):
if '--skip-updown' in sys.argv:
return
# do setup stuff here
and then
`pytest -s -v tests/test_8_2_openpyxl.py --skip-updown
This didn't work and my error message is
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument --skip-updown: expected one argument
I have tried using sys.argv
class TestSomething(unittest.TestCase):
def setUp(self):
if '--skip-updown' in sys.argv:
return
# do setup stuff here
and then
pytest -s -v tests/test_8_2_openpyxl.py --skip-updown
This didn't work and my error message is
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument --skip-updown: expected one argument
I setup a conftest.py
in the project root
def pytest_addoption(parser):
parser.addoption("--skip-updown", default=False)
@pytest.fixture
def skip_updown(request):
return request.config.getoption("--skip-updown")
And then
class TestSomething(unittest.TestCase):
def setUp(self):
if pytest.config.getoption("--skip-updown"):
return
# do setup stuff here and then
pytest -s -v tests/test_8_2_openpyxl.py --skip-updown
Then I get
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument --skip-updown: expected one argument
Exactly the same as before except this time in my command line I declare --skip-updown=True
pytest -s -v tests/test_8_2_openpyxl.py --skip-updown=True
This is very close to what I want, but I was hoping not to have to declare the value --skip-updown=True
Or maybe I am doing it all wrong in the first place and there's an easier way using sys.argv
.
Upvotes: 4
Views: 2486
Reputation: 94483
Fix addoption
:
def pytest_addoption(parser):
parser.addoption("--skip-updown", action='store_true')
See the docs at https://docs.python.org/3/library/argparse.html
Or maybe I am doing it all wrong in the first place and there's an easier way using sys.argv.
No, what you're doing is the right and the only way.
Upvotes: 2