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?
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:
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])
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.)
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