In pytest, you can pass command line arguments by using the pytest_addoption
fixture.
Here is an example:
# conftest.py
def pytest_addoption(parser):
parser.addoption("--url", action="store", default="http://localhost", help="base URL for the tests")
# test_sample.py
def test_example(pytestconfig):
base_url = pytestconfig.getoption("url")
assert base_url == "http://example.com"
In this example, the pytest_addoption
fixture is used to define a command line option --url
. The value of this option can be retrieved in the tests using the pytestconfig
fixture.
To run the test with the --url
option, you would use the following command:
pytest --url=http://example.com
Asked: 2023-07-17 10:37:22 +0000
Seen: 12 times
Last updated: Jul 17 '23