我必须在我存储在 pytest 中的 pytest 命令中传递一个参数addoption
。我想在 pytest 中使用这些值parametrize
功能。
Command:
pytest --range-from 3 --range-to 5 test.py
测试.py:
def pytest_addoption(parser):
parser.addoption("--range-from", action="store", default="default name")
parser.addoption("--range-to", action="store", default="default name")
test.py:
@pytest.mark.parametrize('migration_id', migration_list[range-from:range-to])
def test_sync_with_migration_list(migration_id):
migration_instance = migration.parallel(migration_id=migration_id)
migration_instance.perform_sync_only()
我想使用的值range-from
and range-to
inside parametrize
.
我无法使用这些值。请建议如何做到这一点。
一种简单的方法是将命令行参数分配给环境变量并在任何您想要的地方使用。我不确定您想以哪种方式使用变量,所以在这里我将简单的打印语句放入测试中。
测试.py
def pytest_addoption(parser):
parser.addoption("--range-from", action="store", default="default name") #Let's say value is :5
parser.addoption("--range-to", action="store", default="default name") #Lets's say value is 7
def pytest_configure(config):
os.environ["range_from"]=config.getoption("range-from")
os.environ["range_to"]=config.getoption("range-to")
test.py:
@pytest.mark.parametrize('migration_id', [os.getenv("range_from"),os.getenv("range_to")])
def test_sync_with_migration_list(migration_id):
print(migration_id)
Output :
5
7
希望它会有所帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)