1. The way to execute test cases through testsuit: What if you only want to use a certain use case? How to do? Use the suite and pass in the method name of the test case
import unittest
class UCTestCase ( unittest. TestCase) :
def setUp ( self) :
def tearDown ( self) :
def test1 ( self) :
def test2 ( self) :
if __name__ == "__main__" :
suite = unittest. TestSuite( )
suite. addTest( UCTestCase( "test1" ) )
suite. addTest( UCTestCase( "test2" ) )
runner = unittest. TextTestRunner( )
runner. run( suite)
2. Through the testLoader method: If there are multiple classes, I want to run the test cases under a certain class? Pass testLoader
import unittest
class TestCase1 ( unittest. TestCase) :
def testCase1 ( self) :
print ( "a" )
def testCase2 ( self) :
print ( "b" )
class TestCase2 ( unittest. TestCase) :
def testCase1 ( self) :
print ( "a1" )
def testCase2 ( self) :
print ( "b1" )
if __name__ == "__main__" :
suite1 = unittest. TestLoader( ) . loadTestsFromTestCase( TestCase1)
suite2 = unittest. TestLoader( ) . loadTestsFromTestCase( TestCase2)
suite = unittest. TestSuite( [ suite1, suite2] )
3. Load all test cases under a path through discover
import unittest
test_dir = "./test_case"
discover = unittest. defaultTestLoader. discover( test_dir, pattern= "test*.py" )
if __name__ == "__main__" :
runner= unittest. TextTestRunner( )
runner. run( discover)