如何在循环中运行多个Python测试用例?

时间:2021-03-21 01:21:00

I am new to Python and trying to do something I do often in Ruby. Namely, iterating over a set of indices, using them as argument to function and comparing its results with an array of fixture outputs.

我对Python不熟,并且尝试用Ruby做一些我经常做的事情。也就是说,迭代一组索引,使用它们作为函数的参数,并将其结果与一组fixture输出进行比较。

So I wrote it up like I normally do in Ruby, but this resulted in just one test case.

因此,我像通常在Ruby中那样编写它,但这只导致了一个测试用例。

  def test_output(self):
    for i in range(1,11):
      ....
      self.assertEqual(fn(i),output[i])

I'm trying to get the test for every item in the range. How can I do that?

我试着对范围内的每一项进行测试。我怎么做呢?

3 个解决方案

#1


9  

Using unittest you can show the difference between two sequences all in one test case.

使用unittest,您可以在一个测试用例中显示两个序列之间的差异。

seq1 = range(1, 11)
seq2 = (fn(j) for j in seq1)
assertSequenceEqual(seq1, seq2)

If that's not flexible enough, using unittest, it is possible to generate multiple tests, but it's a bit tricky.

如果这不够灵活,那么使用unittest可以生成多个测试,但是这有点棘手。

def fn(i): ...
output = ...

class TestSequence(unittest.TestCase):
    pass

for i in range(1,11):
    testmethodname = 'test_fn_{0}'.format(i)
    testmethod = lambda self: self.assertEqual(fn(i), output[i])
    setattr(TestSequence, testmethodname, testmethod)

Nose makes the above easier through test generators.

通过测试生成器,Nose使上述操作更加容易。

import nose.tools

def test_fn():
    for i in range(1, 11):
        yield nose.tools.assert_equals, output[i], fn(i)

Similar questions:

类似的问题:

#2


5  

In python world two most popular options to write tests are:

在python世界中,编写测试的两个最流行的选项是:

In pytest you parametrize tests very easly:

在pytest中,您非常容易地参数化测试:

@pytest.mark.parametrize(('param1', 'param2'),[
                         (1, 'go'),
                         (2, 'do not go')])
def test_me(param1, param2):
    # write test

This will produce nice output also while running tests:

这将在运行测试时产生良好的输出:

go.py:2: test_me[1-go] PASSED
go.py:2: test_me[2-do not go] PASSED

I am using pytest for two years now and it's very nice tool. You have many features there. Besides parametrization there are fixtures also, very very nice assertions (you do not need to write assertEqual, just assert a==b and pytest can generate very nice and helpful output for it.)

我使用pytest已经两年了,这是一个很好的工具。这里有很多特性。除了参数化之外,还有fixture,非常好的断言(不需要编写assertEqual,只需断言a==b, pytest可以为它生成非常好的、有用的输出)。

#3


2  

Starting from python 3.4, you can do it like this:

从python 3.4开始,可以这样做:

def test_output(self):
    for i in range(1,11):
        with self.subTest(i=i):
            ....
            self.assertEqual(fn(i),output[i])

https://docs.python.org/3.4/library/unittest.html?highlight=subtest#distinguishing-test-iterations-using-subtests

https://docs.python.org/3.4/library/unittest.html?highlight=subtest distinguishing-test-iterations-using-subtests

#1


9  

Using unittest you can show the difference between two sequences all in one test case.

使用unittest,您可以在一个测试用例中显示两个序列之间的差异。

seq1 = range(1, 11)
seq2 = (fn(j) for j in seq1)
assertSequenceEqual(seq1, seq2)

If that's not flexible enough, using unittest, it is possible to generate multiple tests, but it's a bit tricky.

如果这不够灵活,那么使用unittest可以生成多个测试,但是这有点棘手。

def fn(i): ...
output = ...

class TestSequence(unittest.TestCase):
    pass

for i in range(1,11):
    testmethodname = 'test_fn_{0}'.format(i)
    testmethod = lambda self: self.assertEqual(fn(i), output[i])
    setattr(TestSequence, testmethodname, testmethod)

Nose makes the above easier through test generators.

通过测试生成器,Nose使上述操作更加容易。

import nose.tools

def test_fn():
    for i in range(1, 11):
        yield nose.tools.assert_equals, output[i], fn(i)

Similar questions:

类似的问题:

#2


5  

In python world two most popular options to write tests are:

在python世界中,编写测试的两个最流行的选项是:

In pytest you parametrize tests very easly:

在pytest中,您非常容易地参数化测试:

@pytest.mark.parametrize(('param1', 'param2'),[
                         (1, 'go'),
                         (2, 'do not go')])
def test_me(param1, param2):
    # write test

This will produce nice output also while running tests:

这将在运行测试时产生良好的输出:

go.py:2: test_me[1-go] PASSED
go.py:2: test_me[2-do not go] PASSED

I am using pytest for two years now and it's very nice tool. You have many features there. Besides parametrization there are fixtures also, very very nice assertions (you do not need to write assertEqual, just assert a==b and pytest can generate very nice and helpful output for it.)

我使用pytest已经两年了,这是一个很好的工具。这里有很多特性。除了参数化之外,还有fixture,非常好的断言(不需要编写assertEqual,只需断言a==b, pytest可以为它生成非常好的、有用的输出)。

#3


2  

Starting from python 3.4, you can do it like this:

从python 3.4开始,可以这样做:

def test_output(self):
    for i in range(1,11):
        with self.subTest(i=i):
            ....
            self.assertEqual(fn(i),output[i])

https://docs.python.org/3.4/library/unittest.html?highlight=subtest#distinguishing-test-iterations-using-subtests

https://docs.python.org/3.4/library/unittest.html?highlight=subtest distinguishing-test-iterations-using-subtests