Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 384
picardo Avatar asked Sep 28 '13 20:09

picardo


People also ask

How do you run a test case in a loop in Python?

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.

How do you test multiple test cases?

We can run multiple test cases using TestNG test suite in Selenium webdriver. To execute test cases simultaneously, we have to enable parallel execution in TestNG. A TestNG execution is driven by the TestNG xml file. To trigger parallel execution we have to use the attributes – parallel and thread-count.


1 Answers

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

like image 193
Antoine Fontaine Avatar answered Sep 21 '22 17:09

Antoine Fontaine