在命令行中指定多条测试用例时,如果某个测试用例包含多组参数化数据,pytest 会先完成该用例的所有数据组合的执行后,再开始执行下一条用例。例如,对于前面提到的 test_login 和 test_logout 测试用例,假设 test_login 包含 4 组参数化数据,pytest 会依次执行完这 4 组数据的测试后,才会继续执行 test_logout。

@pytest.mark.login
@pytest.mark.parametrize("login_data", ['cn_test', 'us_test', 'us', 'eu'], indirect=True)
def test_login(appium_service, ios_driver, login_data):
@pytest.mark.logout
def test_logout(appium_service, ios_driver):

像下面这样指定后,并不会先执行完一条数据就紧接着执行test_logout,

testmanzhang@TestMandeMBP testcases % pytest -m "login or logout" test_user_center.py

这里我尝试使用dependency,但是效果仍然是先执行完成测试数据在最后执行test_logout

@pytest.mark.dependency(depends=["test_login"])
def test_logout(appium_service, ios_driver):

所以遇到这种情况要想顺利的执行test_login的4组测试数据,有以下几种解决方法:

1、在test_login中包含登出逻辑;

2、在test_login的末尾,直接引用test_logout;

3、创建fixture,在yield后面 添加登出逻辑;

4、创建fixture,在yield后面 直接引用test_logout;

更多推荐