Ask Your Question
0

What is the method for passing command line arguments in pytest?

asked 2023-07-17 10:37:22 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-17 10:55:02 +0000

djk gravatar image

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
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-17 10:37:22 +0000

Seen: 12 times

Last updated: Jul 17 '23