一个鼻子插件来指定单元测试执行的顺序 [英] A Nose plugin to specify the order of unit test execution
问题描述
我希望将Nose用于有线集成测试套件.但是,其中一些测试的执行顺序很重要.
I have a desire to use Nose for an over the wire integration test suite. However, the order of execution of some of these tests is important.
也就是说,我想我会把一个快速的插件扔在一起,用我想要执行的顺序来装饰测试: https://gist.github.com/Redsz/5736166
That said, I thought I would toss together a quick plugin to decorate a test with the order I want it executed: 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
通过查看我曾经想到的内置插件,我可以简单地覆盖loadTestsFromTestCase
并按修饰的步骤编号"对测试进行排序.
From reviewing the built in plugins I had thought, I could simply override loadTestsFromTestCase
and order the tests by the decorated 'step number'.:
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
此方法将按我想要的顺序返回测试,但是当鼻子执行测试时,不是按此顺序执行测试吗?
This method is returning the tests in the order I desire, however when the tests are being executed by nose they are not being executed in this order?
也许我需要将订购的概念转移到其他位置?
Perhaps I need to move this concept of ordering to a different location?
更新:根据我的评论,该插件实际上按预期运行.我被误认为是pycharm测试记者.测试正在按预期运行.与其删除我认为的问题,不如将其遗忘.
UPDATE: As per the comment I made, the plugin is actually working as expected. I was mistaken to trust the pycharm test reporter. The tests are running as expected. Rather than removing the question I figured I would leave it up.
推荐答案
来自文档:
[...]鼻子按它们在模块文件中出现的顺序运行功能测试.来自TestCase的测试和其他测试类以字母顺序运行.
[...] nose runs functional tests in the order in which they appear in the module file. TestCase-derived tests and other test classes are run in alphabetical order.
一个简单的解决方案可能是重命名测试用例中的测试:
So a simple solution might be to rename the tests in your test case:
class Foo(unittest.TestCase):
def test_01_foo(self):
pass
def test_02_boo(self):
pass
这篇关于一个鼻子插件来指定单元测试执行的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!