我有一个用于Selenium测试的工作pytest环境。我在conftest.py中使用了一个参数化的fixture,它允许我测试所有不同的浏览器,而不必重写测试。我想将命令行参数传递给我的脚本,以便我可以让它只运行特定的浏览器,而不是所有浏览器。要做到这一点,我需要修改传递给我的fixture的变量。到目前为止,我还没弄清楚如何做到这一点。我的下面示例不起作用,可能是因为pytest从我的启动脚本描述的变量中单独解析conftest.py。
#conftest.py
browsers = { "ff" : webdriver.Firefox, "ie" : webdriver.Ie }
@pytest.yield_fixture(params=browsers.keys())
def browser(request):
driver = browsers[request.param]()
yield driver
driver.quit()
#test_simple.py
def test_simple(browser):
browser.get("http://stackoverflow.com")
#main.py
browsers = {}
if __name__ == "__main__":
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
if arg == "-ff":
browsers = { "ff" : webdriver.Firefox }
elif arg == "-ie":
browsers = { "ie" : webdriver.Ie }
elif arg == "-all":
browsers = { "ff" : webdriver.Firefox, "ie" : webdriver.Ie }
pytest.main()
如何将值传递给conftest.py中的参数化夹具?
答案 0 :(得分:1)
请参阅:Pass different values to a test function, depending on command line options。
您无法使用命令行选项实际更改参数化,因为在导入期间会发生参数化定义,但如果用户在命令行中指定了一个浏览器,则可以轻松跳过其他浏览器的测试:
# conftest.py
import pytest
browsers = {"ff": 'FIREFOX', 'ie': 'INTERNETEXPLORER'}
def pytest_addoption(parser):
parser.addoption("--browser", default='',
type='choice', choices=sorted(browsers),
help="runs tests only for given browser")
@pytest.yield_fixture(params=browsers.keys())
def browser(request):
selected = request.config.getoption('browser')
if selected and selected != request.param:
pytest.skip('browser {} selected in the command line'.format(selected))
driver = browsers[request.param]
yield driver
有了这个,当用户运行pytest
而没有将任何值传递给--browser
时,所有测试都会照常运行:
============================= test session starts =============================
platform win32 -- Python 2.7.6 -- py-1.4.26 -- pytest-2.7.0.dev1 -- X:\temp\sandbox\.env27\Scripts\python.exe
plugins: xdist
collecting ... collected 2 items
test_simple.py::test_simple[ie] PASSED
test_simple.py::test_simple[ff] PASSED
========================== 2 passed in 0.01 seconds ===========================
但是如果用户例如通过--browser=ie
,则会跳过firefox测试:
============================= test session starts =============================
platform win32 -- Python 2.7.6 -- py-1.4.26 -- pytest-2.7.0.dev1 -- X:\temp\sandbox\.env27\Scripts\python.exe
plugins: xdist
collecting ... collected 2 items
test_simple.py::test_simple[ie] PASSED
test_simple.py::test_simple[ff] SKIPPED
===================== 1 passed, 1 skipped in 0.01 seconds =====================
答案 1 :(得分:0)
只需使用ENV变量:
#conftest.py
browsers = { "ff" : webdriver.Firefox, "ie" : webdriver.Ie }
@pytest.yield_fixture(params=browsers.keys())
def browser(request):
driver = browsers[request.param]()
yield driver
driver.quit()
#test_simple.py
def test_simple(browser):
browser.get("http://stackoverflow.com")
#main.py
browsers = {}
browserToRun = os.environ.get('BROWSER_TO_RUN')
if __name__ == "__main__":
if browserToRun == "ff":
browsers = { "ff" : webdriver.Firefox }
elif browserToRun == "ie":
browsers = { "ie" : webdriver.Ie }
elif browserToRun == "all":
browsers = { "ff" : webdriver.Firefox, "ie" : webdriver.Ie }
pytest.main()
并运行以下测试:
BROWSER_TO_RUN=ff run_tests.py
答案 2 :(得分:0)
pytest -k test_simple['ie']
以上行将选择性地运行匹配的测试