Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I group test methods and/or test classes in Python unittest

Coming from PHPUnit, it was easy to group test classes or functions using the@group annotation. That way I could run or exclude a very particular subset of tests, potentially across multiple files.

I'm wondering if python unittest has something similar. If that is the case how do I use it and run it from the CLI?

Thanks.

like image 534
Onema Avatar asked Nov 25 '15 21:11

Onema


1 Answers

It is possible to run a group of test functions by putting them all in a single class. Let's say you have 4 test functions in your unittest and you want to have two groups of 2 functions. You need to create a tests.py script with two classes, each of which having 2 functions:

from unittest import TestCase
class testClass1(TestCase):
   def testFunction1(self):
      #test something
   def testFunction2(self):
      #test something
class testClass2(TestCase):
   def testFunction3(self):
      #test something
   def testFunction4(self):
      #test something

Then to run your unittest only on class 1 from command line, you can run

python -m unittest tests.testClass1
like image 165
shahram kalantari Avatar answered Sep 27 '22 20:09

shahram kalantari