您好,我使用 pytest 并在文件夹中有以下 2 个 py 文件。
test_abc.py如下:
class MyTest(unittest.TestCase):
@classmethod
def setup_class(cls):
cls.a = 10
@classmethod
def teardown_class(cls):
cls.a = 20
@pytest.mark.run(order=2)
def test_method1(self):
logging.warning('order2 in test_abc')
assert (10,self.a) # fail for demo purposes
@pytest.mark.run(order=1)
def test_method2(self):
logging.warning('order1 in test_abc')
assert 0, self.db # fail for demo purposes
test_sample2.py 如下,
class MyTest1(unittest.TestCase):
@classmethod
def setup_class(cls):
cls.a = 10
@classmethod
def teardown_class(cls):
cls.a = 20
@pytest.mark.run(order=2)
def test_mtd1(self):
logging.warning('order2 in test_samp')
assert (10,self.a) # fail for demo purposes
@pytest.mark.run(order=1)
def test_mtd2(self):
logging.warning('order1 in test_samp')
assert 0, self.db # fail for demo purposes
现在我使用命令运行:
py.test --tb=long --junit-xml=results.xml --html=results.html -vv
这里发生的情况是两个测试用例文件中的 test_method2 首先运行(因为它已作为 order1 给出),然后 test_method1 从两个文件运行(因为它已作为 order 2 给出)
所以我在这里注意到的是,排序是针对测试运行的总体排序,而不是针对单个类/文件
有什么办法可以解决这个问题吗?现在,我对所有文件都使用订购号,例如我给出的第一个文件(1,2),然后在下一个文件中我给出的(3,4),它工作正常。
但我不想只在我需要的少数地方订购所有测试课程。是否有任何钩子说 pytest 仅查看特定文件中的排序?