简体   繁体   English

python unittest中的测试顺序

[英]Order of tests in python unittest

I was looking at similar questions and I couldn't find an answer to my problem. 我正在寻找类似的问题,但找不到我的问题的答案。

I wrote Tests in a python class that derives from unittest.TestCase 我在从unittest.TestCase派生的python类中编写了Tests

class TestEffortFormula(unittest.TestCase)

I need to give an order to the tests (please, do not tell me that I shouldn't rely on test's order, I just do). 我需要给一个为了测试(请不要告诉我,我不应该依赖测试的顺序,我只是做)。

Before I needed to give order to the tests the command I used to run the tests was: 在我需要对测试进行排序之前,我用来运行测试的命令是:

unittest.main(testRunner=TeamcityTestRunner())

Then I wanted to make the order dissappear, so I tried the following: 然后我想使订单消失,所以我尝试了以下操作:

loader = unittest.TestLoader()
loader.sortTestMethodsUsing(None)
loader.loadTestsFromTestCase(TestEffortFormula)
suite = loader.suiteClass()

but from here I don't know how to run the tests, specially with testRunner=TeamcityTestRunner() as I did before. 但是从这里开始,我不知道如何运行测试,特别是像以前一样使用testRunner=TeamcityTestRunner()进行测试。

Appreciate your help 感谢你的帮助

Option 1. 选项1。

One solution to this (as a workaround) was given here - which suggests writing the tests in numbered methods step1 , step2 , etc., then collecting and storing them via dir(self) and yielding them to one test_ method which try s each. 在此给出了一种解决方案(作为一种解决方法)-建议使用编号为step1step2等的方法编写测试,然后通过dir(self)收集和存储测试,并将其提供给一个test_方法,每个try s。

Not ideal but does what you expect. 不理想,但您期望的结果。 Each test sequence has to be a single TestClass (or adapt the method given there to have more than one sequence generating method). 每个测试序列必须是单个TestClass(或使那里给出的方法适用于具有多个序列生成方法)。

Option 2. 选项2。

Another solution, also in the linked question, is you name your tests alphabetically+numerically sorted so that they will execute in that order. 另一个解决方案,也是在链接的问题中,是您按字母顺序和数字顺序命名测试,以便它们按该顺序执行。

But in both cases, write monolithic tests, each in their own Test Class. 但是在两种情况下,都编写整体测试,每个测试都在自己的测试类中。

PS I agree with all the comments that say unit testing shouldn't be done this way; PS:我同意所有的意见,即不应以这种方式进行单元测试。 but there are situations where unit test frameworks (like unittest and pytest ) get used to do integration tests which need modular independent steps to be useful. 但是在某些情况下,单元测试框架(例如unittestpytest )习惯于进行集成测试,这需要模块化的独立步骤才有用。 Also, if QA can't influence Dev to write modular code, these kinds of things have to be done. 另外,如果质量检查不能影响Dev编写模块化代码,则必须完成这些事情。

I've searched a long time to solve this problem myself. 我已经搜寻了很长时间来自己解决这个问题。
One of the answers in this question does exactly what you need. 该问题的答案之一正是您需要的。

Applied to your code: 应用于您的代码:

ln = lambda f: getattr(TestEffortFormula, f).im_func.func_code.co_firstlineno
lncmp = lambda _, a, b: cmp(ln(a), ln(b))
unittest.TestLoader.sortTestMethodsUsing = lncmp

suite = unittest.TestLoader().loadTestsFromTestCase(TestEffortFormula)
unittest.TextTestRunner(failfast=True).run(suite)

Unfortunately, setting unittest.TestLoader.sortTestMethodsUsing=None does not work, although it is documented that this should avoid sorting the tests alphabetically. 不幸的是,设置unittest.TestLoader.sortTestMethodsUsing=None不起作用,尽管有文献证明这应该避免按字母顺序对测试进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM