1. 通过testsuit来执行测试用例的方式: 如果想只想运用某一个用例呢?怎么办?使用套件然后传入测试用例的方法名即可
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. 通过testLoader方式: 如果有多个类的情况下,我想运行某个类下面的测试用例呢?通过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. 通过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)