Python连续运行单元测试或每个测试多次 [英] Python run unittests continuously or each test multiple times

查看:381
本文介绍了Python连续运行单元测试或每个测试多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了单元测试用例来测试我的应用程序.并且它按预期工作,没有出现问题.

I have wrote unit test cases to test my application. and it is working as expected with out issues.

下面是一些测试用例

import os
import unittest

class CreateUser(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass
    def test_send_message(self):
        #my script goes here
        print "success"
if __name__ == '__main__':
    unittest.main()

如果我运行此测试,它会按预期执行,但我想不运行N个测试用例

If i run this test it executing as expected but I want to run this test case 'N' no of times,

为此我在主要功能中添加了for循环,它也只运行了一次,我使用的代码如下所示

for that i added for loop in main function, also its running only one time, code i used as below

 if __name__ == '__main__':
    for i in range(1, 5):
         unittest.main()  

我还使用时间表 lib每10分钟运行一次测试,但没有运气

I also used schedule lib to run test every 10 mins but no luck

是否可以多次运行此测试用例,或者是否缺少任何其他解决方法,或者无法使用任何其他连续构建工具来实现此目的?

Is there any way to run this test case multiple times or any other work around that i am missing or any other continuous build tool to achive this?

预先感谢

推荐答案

首先,请多加注意.

您为什么要运行五次相同的测试?我不想在没有看到您的代码的情况下进行假设,但这是一种非常严重的代码味道.单元测试必须是可重复的,并且如果连续运行五次不能获得与一次运行相同的结果,则该测试中的某些内容是不可重复的.特别是,如果测试的早期运行产生了后续运行所使用的副作用,或者涉及某种随机数,则这两种情况都是非常糟糕的情况,需要修复,而不是多次运行测试.仅鉴于我们在此处获得的信息,最好的建议很有可能是不要多次运行该测试!

Why do you want to run the same test five times? I don't want to make assumptions without seeing your code, but this is a pretty serious code smell. A unit test must be repeatable, and if running it five times in a row does not give the same result as running it once, then something in the test is not repeatable. In particular, if early runs of the test are creating side effects used by later runs, or if there is some kind of random number involved, both of those are very bad situations that need to be repaired rather than running the test multiple times. Given just the information that we have here, it seems very likely that the best advice will be don't run that test multiple times!

话虽如此,您有几种选择.

Having said that, you have a few different options.

  1. 将循环放入测试内部

假设调用一次函数有五次有意义的事情,那么做类似的事情是完全合理的:

Assuming there is something meaningful about calling a function five times, it is perfectly reasonable to do something like:

def test_function_calls(self):
    for _ in xrange(1, 5):
        self.assertTrue(f())

  1. 自从您提到了鼻子标签以来,您就有了参数化测试的一些选项,这些选项通常包括对不同的输入值运行相同的(代码)测试.如果您使用了 https://github.com/wolever/nose-parameterized 之类的东西,那么您的结果可能是这样的:
  1. Since you mentioned the nose tag, you have some options for parameterized tests, which usually consists of running the same (code) test over different input values. If you used something like https://github.com/wolever/nose-parameterized , then your result might be something like this:

@parameterized.expand([('atest', 'a', 1), ('btest', 'b', 2)])
def test_function_calls(self, name, input, expected):
    self.assertEqual(expected, f(input))

顾名思义,

参数化测试通常用于检查一个带有多个数据的代码测试.如果您只想让测试运行几次,则可以有一个包含伪数据的列表,但这是另一个可疑的代码结构,可以追溯到我的初衷.

Parameterized tests are, as the name implies, typically for checking one code test with several pieces of data. You can have a list with dummy data if you just want the test to run several times, but that's another suspicious code structure that goes back to my original point.

奖金附带说明:几乎所有连续"构建工具均已设置为触发构建/测试/等.根据特定条件或事件,例如何时将代码提交到存储库.对于他们来说,连续连续运行测试是非常不寻常的.

Bonus side note: almost all "continuous" build tools are set up to trigger builds/tests/etc. on specific conditions or events, like when code is submitted to a repository. It is very unusual for them to simply run tests continuously.

我已尽力在这里回答您的问题,但我感觉有些东西丢失了.您可能需要确切说明要完成的工作,以获得最佳答案.

I've done my best to answer your question here, but I feel like something is missing. You may want to clarify exactly what you are trying to accomplish in order to get the best answer.

这篇关于Python连续运行单元测试或每个测试多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆