一个Nose插件,用于指定单元测试执行的顺序

Jes*_*sse 15 python nose nosetests

我希望将Nose用于线上集成测试套件.但是,其中一些测试的执行顺序很重要.

那就是说,我想我会把一个快速的插件拼凑起来用我想要它执行的命令来装饰测试:https://gist.github.com/Redsz/5736166

def Foo(unittest.TestCase):

    @step(number=1)
    def test_foo(self):
        pass

    @step(number=2)
    def test_boo(self):
        pass
Run Code Online (Sandbox Code Playgroud)

通过回顾我曾经想过的内置插件,我可以loadTestsFromTestCase通过装饰的"步骤编号" 简单地覆盖和排序测试:

def loadTestsFromTestCase(self, cls):
    """
    Return tests in this test case class. Ordered by the step definitions.
    """
    l = loader.TestLoader()
    tmp = l.loadTestsFromTestCase(cls)

    test_order = []
    for test in tmp._tests:
        order = test.test._testMethodName
        func = getattr(cls, test.test._testMethodName)
        if hasattr(func, 'number'):
            order = getattr(func, 'number')
        test_order.append((test, order))
    test_order.sort(key=lambda tup: tup[1])
    tmp._tests = (t[0] for t in test_order)
    return tmp
Run Code Online (Sandbox Code Playgroud)

这个方法按照我想要的顺序返回测试,但是当测试由nose执行时,它们没有按此顺序执行?

也许我需要将这种订购概念转移到不同的位置?

更新:根据我的评论,插件实际上正在按预期工作.我错误地相信pycharm测试记者.测试按预期运行.而不是删除我想的问题,我会把它留下来.

quo*_*ian 17

文档:

[...] nose按照它们出现在模块文件中的顺序运行功能测试.TestCase派生的测试和其他测试类按字母顺序运行.

因此,一个简单的解决方案可能是在测试用例中重命名测试:

class Foo(unittest.TestCase):

    def test_01_foo(self):
        pass

    def test_02_boo(self):
        pass
Run Code Online (Sandbox Code Playgroud)