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

Question:

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?

Asked By: picardo

||

Answers:

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:

Answered By: Trevor Merrifield

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

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

Answered By: spinus

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

Answered By: Antoine Fontaine

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
Answered By: Sourav Choudhary
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.