简体   繁体   English

如何运行python unittest N次

[英]How to run the python unittest N number of times

I have a python unittest like below , I want to run this whole Test N Number of times我有一个像下面这样的 python unittest,我想运行整个测试 N 次

class Test(TestCase)

    def test_0(self):
            .........
            .........
            .........

Test.Run(name=__name__)

Any Suggestions?有什么建议吗?

You can use parameterized tests.您可以使用参数化测试。 There are different modules to do that.有不同的模块可以做到这一点。 I use nose to run my unittests (more powerful than the default unittest module) and there's a package called nose-parameterized that allows you to write a factory test and run it a number of times with different values for variables you want.我使用nose 来运行我的单元测试(比默认的unittest 模块更强大),并且有一个名为nose-parameterized 的包,它允许您编写工厂测试并使用所需变量的不同值多次运行它。 If you don't want to use nose, there are several other options for running parameterized tests.如果您不想使用鼻子,还有其他几个选项可用于运行参数化测试。

Alternatively you can execute any number of test conditions in a single test (as soon as one fails the test will report error).或者,您可以在单个测试中执行任意数量的测试条件(一旦失败,测试将报告错误)。 In your particular case, maybe this makes more sense than parameterized tests, because in reality it's only one test, only that it needs a large number of runs of the function to get to some level of confidence that it's working properly.在您的特定情况下,这可能比参数化测试更有意义,因为实际上它只是一个测试,只是它需要大量运行该函数才能对其正常工作有一定的信心。 So you can do:所以你可以这样做:

import random
class Test(TestCase)

    def test_myfunc(self):
        for _ in range(100):
            input = random.random() 
            self.assertEquals(input, input + 2)


Test.Run(name=__name__) 

Why because... the test_0 method contains a random option.. so each time it runs it selects random number of configuration and tests against those configurations.为什么因为...... test_0 方法包含一个随机选项......所以每次运行它都会选择随机数量的配置并针对这些配置进行测试。 so I am not testing the same thing multiple times..所以我不会多次测试同一件事。

Randomness in a test makes it non-reproducible.测试中的随机性使其不可重复。 One day you might get 1 failure out of 100, and when you run it again, it's already gone.有一天,您可能会遇到 100 次中的 1 次失败,而当您再次运行它时,它已经消失了。

Use a modern testing tool to parametrize your test with a sequential number, then userandom.seed to have a random but reproducible test case for each number in a sequence.使用现代测试工具用序列号参数化您的测试,然后使用random.seed为序列中的每个数字生成一个随机但可重复的测试用例。

portusato suggests nose, but pytest is a more modern and popular tool: portusato 建议使用鼻子,但 pytest 是一种更现代和流行的工具:

import random, pytest

@pytest.mark.parametrize('i', range(100))
def test_random(i):
    orig_state = random.getstate()
    try:
        random.seed(i)
        data = generate_random_data()
        assert my_algorithm(data) == works
    finally:
        random.setstate(orig_state)

pytest.mark.parametrize “explodes” your single test_random into 100 individual tests — test_random[0] through test_random[99] : pytest.mark.parametrize将您的单个test_randompytest.mark.parametrize ”为 100 个单独的测试 — test_random[0]test_random[99]

$ pytest -q test.py
....................................................................................................
100 passed in 0.14 seconds

Each of these tests generates different, random, but reproducible input data to your algorithm.这些测试中的每一个都会为您的算法生成不同的、随机的但可重现的输入data If test_random[56] fails, it will fail every time , so you will then be able to debug it.如果test_random[56]失败,它每次都会失败,这样你就可以调试它了。

If you don't want your test to stop after the first failure, you can use subTest .如果您不希望您的测试在第一次失败后停止,您可以使用subTest

class Test(TestCase):
    def test_0(self):
        for i in [1, 2, 3]:
            with self.subTest(i=i):
                self.assertEqual(squared(i), i**2)

Docs 文档

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

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