【问题标题】:pytest running scenarios in the correct order in the classpytest 在类中以正确的顺序运行场景
【发布时间】:2012-09-20 22:37:06
【问题描述】:

所以我有以下结构:

class Test(object):

   def test_1(self):
      pass

   def test_2(self):
      pass

   def test_3(self):
      pass

它运行得很好,现在我正在添加“场景”(正如 pytest - A quick port of “testscenarios” 推荐的那样):

def pytest_generate_tests(metafunc):
    idlist = []
    argvalues = []
    for scenario in metafunc.cls.scenarios:
        idlist.append(scenario[0])
        items = scenario[1].items()
        argnames = [x[0] for x in items]
        argvalues.append(([x[1] for x in items]))
    metafunc.parametrize(argnames, argvalues, ids=idlist)

class Test(object):
       scenarios = ['1' {'arg':'value1'},
                    '2' {'arg':'value2'}]

       def test_1(self, arg):
          pass

       def test_2(self, arg):
          pass

       def test_3(self, arg):
          pass

当我运行它时,测试顺序错误,我得到:

test_1[1]  
test_1[2]  
test_2[1]   
test_2[2]  
test_3[1]  
test_3[2]

看起来不像是 Test 类的场景。

问题:是否有任何解决方案可以以正确的顺序运行它?喜欢:

test_1[1]
test_2[1]
test_3[1]
test_1[2]
test_2[2]
test_3[2]

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    即将推出的 pytest-2.3 支持更好的(基于资源的)排序,我刚刚更新了文档中的场景示例:https://docs.pytest.org/en/latest/example/parametrize.html#a-quick-port-of-testscenarios

    您可以使用

    初步安装当前的开发版本
    pip install -i http://pypi.testrun.org -U pytest
    

    并且应该获得带有“py.test --version”的pytest-2.3.0.dev15并且能够使用它。

    【讨论】:

      【解决方案2】:

      py.test 以分布式方式运行测试,这意味着顺序基本上是随机的。

      您应该使用-n 选项并将进程号设置为1。 然后测试应该由生成的单个进程按字母顺序运行。

      除此之外,我不知道你是否可以做到。无论如何,取决于测试的顺序通常是 bad 设计。所以你应该尽量不依赖它。

      【讨论】:

      • 我认为 Alex 在这里并不是指分布式测试,否则我基本同意。
      • @Bakuriu 感谢您的回答,但实际上我指的是同一过程中的测试顺序。还有关于顺序依赖性 - 在我看来,这是非常有争议的:测试类应该是独立的,但是里面的测试方法 - 是我的测试步骤。我使用 pytest 不是用于单元测试,而是用于使用 Selenium webdriver 实现 UI 自动化。
      • 最新的py.test好像不支持-n,有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 2023-01-03
      • 2014-03-22
      • 1970-01-01
      • 2021-02-12
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      相关资源
      最近更新 更多