'' ' Why use unit testing framework? : 1. When a lot of time with embodiment examples and tissues for execution in Example 2. The comparative method provides a wealth of 3. The log provides a rich '' ' Import the unittest Import HTMLTestRunner Import OS path = The os.getcwd () class Test_baidu (of unittest.TestCase ): DEF the setUp (Self): Print ( ' each started ----------- ' ) DEF TEST_1 (Self): Print ( ' TEST_1 ' ) self.assertEqual ( ' . 1 ' , ' 2 ' , MSG =' . 1 = 2! ' ) DEF test_2 (Self): Print ( ' test_2 ' ) self.assertIsInstance ([ 1,2 ], List) @ unittest.skip # Although this method is not executed, but also execution of the setup and teardown DEF test_3 (Self): Print ( ' test_3 ' ) self.assertIs ( ' . 1 ' , ' . 1 ' ) DEF the tearDown (Self): Print ( ' each end ---------- ' ) @ classmethod DEF setUpClass (CLS): Print ( ' big project is to open it +++++++++ ' ) @classmethod DEF tearDownClass (CLS): Print ( ' big project ended ++++++++++ + ' ) IF the __name__ == ' __main__ ' : SUIT = unittest.TestSuite () suit.addTest (Test_baidu ( ' test_2 ' )) # order to be executed first, added into a --- is a tissue with Example suit.addTest (Test_baidu ( ' TEST_1 ' )) suit.addTest (Test_baidu ( 'test_3 ' )) # With skip existence, but also added to the list of no use Runner = unittest.TextTestRunner () # After adding the case to be executed to suit, with TextTestRunner () object can run - Organization handy Cases performing a key # Discover unittest.TestLoader = (). Discover (path) # runner1 HTMLTestRunner.HTMLTestRunner = () # runner1.run (Discover) runner.run (SUIT)
The above is a test * .py ,, this is main.py
'' ' When used in Example rarely a suit can add one. We must all use cases are on TEST_Case role, there are many test * .py, now only a u1.py '' ' Import unittest, os path = os.getcwd () Print (path) the Discover = unittest.defaultTestLoader. the Discover (path, pattern = ' U * .py ' ) # the Discover return is the suit, so Testloader all cases is to be loaded into the suit. Runner = unittest.TextTestRunner () runner.run (Discover) IF the __name__ == ' __main__ ' : unittest.main (the verbosity =. 1)
skip skip test
import unittest import HTMLTestRunner import os path = os.getcwd() class Test_baidu(unittest.TestCase): def setUp(self): print('每一个开始了-----------') def test_1(self): print('test_1') self.assertEqual('1','2',msg='1!=2') def test_2(self): print(' Test_2 ' ) self.assertIsInstance ([ 1,2 ], List) @ unittest.skip # Although this method is not performed, but also perform setup and teardown of DEF test_3 (Self): Print ( ' test_3 ' ) self.assertIs ( ' . 1 ' , ' . 1 ' ) @ unittest.skipIf ( . 3 <2, ' is a true condition when the test is skipped, performed when the condition is false case ' ) DEF test_4 (Self): Print ( ' test_4 ' ) Self .assertIs ( '. 1 ' , ' . 1 ' ) @ unittest.skipUnless ( . 3 <2, ' when the condition is not performed when false test ' ) # The unless unless DEF TEST_5 (Self): Print ( ' TEST_5 ' ) self.assertIs ( ' . 1 ' , ' . 1 ' ) DEF the tearDown (Self): Print ( ' each end ---------- ' ) @classmethod DEF setUpClass (CLS): Print ( 'Large projects it is open +++++++++ ' ) @classmethod DEF tearDownClass (CLS): Print ( ' big project ended +++++++++++ ' ) IF __name__ == ' __main__ ' : unittest.main (the verbosity = 2)
Generate html file
Import the unittest, OS from HTMLTestRunner Import HTMLTestRunner path = The os.getcwd () Print (path) Discover = unittest.defaultTestLoader.discover (path, pattern = ' U * .py ' ) # Discover is returned suit, so that all the Testloader case to suit the load. htmlpath = R & lt path + ' \ report.html ' Print (htmlpath) F = Open (htmlpath, ' WB ' ) Runner = HTMLTestRunner (F = Stream, the verbosity = 2, title = ' test report ' , Description = 'The test results of their own ' ) # htmltestrunner package is to testrunner runner.run (Discover) f.close () IF the __name__ == ' __main__ ' : unittest.main (the verbosity =. 1)
Import Time now = time.strftime ( " % Y-% M-% d% H-% M-% S " ) '' ' original html file name is written dead, regenerate will cover all let each joined name is not the same (file name does not seem to: have an error) '' ' htmlpath = ' F: / ASUS / auto_file / unittest_html ' + ' / ' + now + ' report.html ' f = Open (htmlpath, ' wb ' ) Runner = HTMLTestRunner (Stream = f, verbosity = 2, title = ' test report ' , the Description = ' this is their test results ' ) #htmltestrunner package is to testrunner runner.run (Discover) f.close ()