用不同的参数运行测试套件?(pytest)



在一个类中有一组UI测试:

类TestMainPage(对象(:login='login'password='password'

def test_open_site(self, browser, url):
"""Opening browser and go to website"""
browser.go_to_site(url)
browser.check_open_auth_page()

def test_authorisation(self, browser):
"""Аuthorization on the website"""
browser.authorisation(login=self.login, password=self.password)
...

如何使用不同的参数依次为一个测试会话运行这个类?该功能的参数化是不合适的,因为该测试将独立执行,并且只有在之后才能进行下一次测试

我找到了这个解决方案:

my_list = [1,2,3]

@pytest.fixture(scope="session", params=my_list)
def my_params(request):
return request.param

class Test_suite:
def test_1(self, my_params):
assert 1==1
def test_2(self, my_params, ):
assert 1 == 1

def test_3(self, my_params, ):
assert 1 == 1

它是交替执行的,这正是我所需要的:

test.py::testronguite::test_1[1]

test.py::testronguite::test_2[1]

test.py::testronguite::test_3[1]

test.py::testronguite::test_1[2]

test.py::testronguite::test_2[2]

test.py::testronguite::test_1[3]

test.py::testronguite::test_2[3]

test.py::testronguite::test_3[3]

但当添加第二个参数时,混乱开始(实际结果(:

test.py::testronguite::test_1[1]通过

test.py::testronguite::test_2[1-1]通过

test.py::testronguite::test_3[1-1]通过

test.py::testronguite::test_2[2-1]通过

test.py::testronguite::test_3[2-1]通过

test.py::testronguite::test_1[2]通过

test.py::testronguite::test_2[2-2]通过

test.py::testronguite::test_3[2-2]通过

test.py::testronguite::test_2[1-2]通过

test.py::testronguite::test_3[1-2]通过

test.py::testronguite::test_2[3-2]通过

test.py::testronguite::test_3[3-2]通过

test.py::testronguite::test_2[3-1]通过

test.py::testronguite::test_3[3-1]通过

test.py::testronguite::test_1[3]通过

test.py::testronguite::test_2[3-3]通过

我还需要一个有序的结论(预期结果(:

test.py::testronguite::test_1[1]通过

test.py::testronguite::test_2[1-1]通过

test.py::testronguite::test_3[1-1]通过

test.py::testronguite::test_2[1-2]通过

test.py::testronguite::test_3[1-2]通过

test.py::testronguite::test_2[1-3]通过

test.py::testronguite::test_3[1-3]通过

test.py::testronguite::test_2[1-4]通过

test.py::testronguite::test_3[1-4]通过

test.py::testronguite::test_2[1-5]通过

test.py::testronguite::test_3[1-5]通过

test.py::testronguite::test_1[2]通过

test.py::testronguite::test_2[2-1]通过

test.py::testronguite::test_3[2-1]通过

还有更多。如何实施?

使用了带有两个变量的脚本:

my_list = [1,2,3]
my_list_two = [1,2,3,4,5]
@pytest.fixture(scope="session", params=my_list)
def my_params(request):
return request.param
@pytest.fixture(scope="session", params=my_list_two)
def my_params_two(request):
return request.param

class Test_suite:
def test_1(self, my_params):
assert 1==1
def test_2(self, my_params, my_params_two):
assert 1 == 1

def test_3(self, my_params, my_params_two):
assert 1 == 1

快速的谷歌搜索会让你找到这个问题,其中列出了一些解决方案,其中包括移动一个固定装置的范围(见下文(或使用一些pytest_modify_collection钩子,但你遇到的问题似乎仍然是一个悬而未决的问题

import pytest
LOGINS = ["l1", "l2", "l3"]
PASSWORDS = ["p1", "p2", "p3", "p4", "p5"]
@pytest.fixture(scope="session", params=LOGINS)
def logins(request):
return request.param

@pytest.fixture(scope="module", params=PASSWORDS)
def passwords(request):
return request.param

def test_one(logins):
pass
def test_two(logins, passwords):
pass

在这种特殊情况下,如果您将固定装置的范围更改为function,您将得到您想要的结果:

import pytest
my_list = [1, 2, 3]
my_list_two = [1, 2, 3, 4, 5]

@pytest.fixture(params=my_list)
def my_params(request):
return request.param

@pytest.fixture(params=my_list_two)
def my_params_two(request):
return request.param

class TestSuite:
def test_1(self, my_params):
assert 1 == 1
def test_2(self, my_params, my_params_two):
assert 1 == 1
def test_3(self, my_params, my_params_two):
assert 1 == 1
test_suite.py::TestSuite::test_1[1] PASSED                                                                                                                                                                                           [  3%]
test_suite.py::TestSuite::test_1[2] PASSED                                                                                                                                                                                           [  6%]
test_suite.py::TestSuite::test_1[3] PASSED                                                                                                                                                                                           [  9%]
test_suite.py::TestSuite::test_2[1-1] PASSED                                                                                                                                                                                         [ 12%]
test_suite.py::TestSuite::test_2[1-2] PASSED                                                                                                                                                                                         [ 15%]
test_suite.py::TestSuite::test_2[1-3] PASSED                                                                                                                                                                                         [ 18%]
test_suite.py::TestSuite::test_2[1-4] PASSED                                                                                                                                                                                         [ 21%]
test_suite.py::TestSuite::test_2[1-5] PASSED                                                                                                                                                                                         [ 24%]
test_suite.py::TestSuite::test_2[2-1] PASSED                                                                                                                                                                                         [ 27%]
test_suite.py::TestSuite::test_2[2-2] PASSED                                                                                                                                                                                         [ 30%]
test_suite.py::TestSuite::test_2[2-3] PASSED                                                                                                                                                                                         [ 33%]
test_suite.py::TestSuite::test_2[2-4] PASSED                                                                                                                                                                                         [ 36%]
test_suite.py::TestSuite::test_2[2-5] PASSED                                                                                                                                                                                         [ 39%]
test_suite.py::TestSuite::test_2[3-1] PASSED                                                                                                                                                                                         [ 42%]
test_suite.py::TestSuite::test_2[3-2] PASSED                                                                                                                                                                                         [ 45%]
test_suite.py::TestSuite::test_2[3-3] PASSED                                                                                                                                                                                         [ 48%]
test_suite.py::TestSuite::test_2[3-4] PASSED                                                                                                                                                                                         [ 51%]
test_suite.py::TestSuite::test_2[3-5] PASSED                                                                                                                                                                                         [ 54%]
test_suite.py::TestSuite::test_3[1-1] PASSED                                                                                                                                                                                         [ 57%]
test_suite.py::TestSuite::test_3[1-2] PASSED                                                                                                                                                                                         [ 60%]
test_suite.py::TestSuite::test_3[1-3] PASSED                                                                                                                                                                                         [ 63%]
test_suite.py::TestSuite::test_3[1-4] PASSED                                                                                                                                                                                         [ 66%]
test_suite.py::TestSuite::test_3[1-5] PASSED                                                                                                                                                                                         [ 69%]
test_suite.py::TestSuite::test_3[2-1] PASSED                                                                                                                                                                                         [ 72%]
test_suite.py::TestSuite::test_3[2-2] PASSED                                                                                                                                                                                         [ 75%]
test_suite.py::TestSuite::test_3[2-3] PASSED                                                                                                                                                                                         [ 78%]
test_suite.py::TestSuite::test_3[2-4] PASSED                                                                                                                                                                                         [ 81%]
test_suite.py::TestSuite::test_3[2-5] PASSED                                                                                                                                                                                         [ 84%]
test_suite.py::TestSuite::test_3[3-1] PASSED                                                                                                                                                                                         [ 87%]
test_suite.py::TestSuite::test_3[3-2] PASSED                                                                                                                                                                                         [ 90%]
test_suite.py::TestSuite::test_3[3-3] PASSED                                                                                                                                                                                         [ 93%]
test_suite.py::TestSuite::test_3[3-4] PASSED                                                                                                                                                                                         [ 96%]
test_suite.py::TestSuite::test_3[3-5] PASSED                                                                                                                                                                                         [100%]

有关如何订购固定装置的更多信息,请参阅文档。

事实证明,这个问题目前还没有解决。这个问题的唯一(非官方(解决方案是使用优先级参数。此功能在pytest param优先级库pytest parampriority:中实现

import pytest
import time
from pytest_param_priority import parameter_priority

@parameter_priority(0)
@pytest.fixture(scope="module", params=[1, 2, 3])
def number(request):
return request.param
@parameter_priority(2)
@pytest.fixture(scope="module", params=["a", "b", "c"])
def letter(request):
return request.param
@pytest.fixture(scope="module")
def my_expensive_setup_fixture(letter, number):
time.sleep(1)
@parameter_priority(1)
@pytest.fixture(scope="module", params=["red", "green", "blue"])
def color(request):
return request.param
@pytest.fixture(scope="module")
def intermediate_step(color, my_expensive_setup_fixture):
pass
def test_one(intermediate_step):
pass
def test_two(intermediate_step):
pass

管理设置测试的优先级不再是的问题

我将尝试实现一个类似的系统,并测试它的结果。稍后,我将为我的代码添加一个专门的解决方案,也许它对有用

最新更新