How do I run multiple Python test cases in a loop?

拟墨画扇 提交于 2019-12-03 08:36:04

问题


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.

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

  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?


回答1:


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

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.

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.

import nose.tools

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

Similar questions:

  • Python unittest: Generate multiple tests programmatically?
  • How to generate dynamic (parametrized) unit tests in python?



回答2:


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

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




回答3:


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

  • Unittest
  • pytest

In pytest you parametrize tests very easly:

@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.)




回答4:


If Your question is regarding, when you are solving the problem on competitive places like hackerrank or anywhere else. If they have not provided their environment to run test cases in a loop.

And locally if You are running code for python compiler It would be useful.

you can simply use a while loop or range function of python.

e.g:

t = int(input("Enter Number of testcases"))
type(t)

while(t!=0):

    n = int(input("Enter number of data"))
    type(n)

    // Code logic or function Call

    t = t-1


来源:https://stackoverflow.com/questions/19071601/how-do-i-run-multiple-python-test-cases-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!