python_ unit testing unittest (1)

'' '' '' 
'' '
1, Run
1, right click
2, in operation Terminal (console):
first adding
IF __name__ __ ==' __ main__ ':
unittest.main ()
Terminal (console) enter the command: python <module name .py>

2, the test suite (the TestSuite)
command (all) execution: python <module name .py> -m unittest
single execution: python -m unittest <module name class name.>
Python -m unittest <module name. class name. method name>


3, loading (TestLoader)
need to initialize an object, suite test suite (a collection of)
3.1 - a first method)
# initialization test suite
Suite unittest.TestSuite = ()

# to test kit which added test, first import module
cases = [class name ( "method name"), the class name ( 'method name')]
suite.addTests (Cases)

. 3.2-- second method)
Suite = unittest.TestSuite (= Cases Tests)


4, run, test report file,

first open a file
with Open ( 'TXT', 'w', encoding = 'utf8') AS f:
# initialize runner, logging detail level, slightly -> fine (0, 1 (default ), 2)
Runner = unittest.TextTestRunner (F, the verbosity = 0)
# run
runner.run (Suite)

. 5, the execution order with the unittest embodiment is a method name, in particular according to the ASCII code
, if according to the ASCII code, can add value
def test_add_1_success (Self):
Pass
DEF test_add_2_success (Self):
Pass

. 6, the setUp attention to the case and the tearDown #
DEF the setUp (Self):
# preconditions
automatically executed inside the program setup before the test method #
def TearDown (self):
after # postcondition
automatically executed inside the program after Teardown test method #


7, TestLoader used to load test
# loading module can also be tested in accordance with class loading, own rules to automatically load;
7.1) initialization loader
= unittest.TestLoader Loader ()
# test class according to load test
cases = loader.loadTestFromTestCase (class name)
Suite1 = unittest.TestSuite ()
suite1.addTests (Cases)

. 8, the test case automatic discovery (Discover)
# = the unittest Suite .TestSuite ()
# initialization Loader
Loader unittest.Testloader = ()
# automatic discovery test
start_dir = os.path.dirname (os.path.abspath with (__ file__))
Suite1 = loader.discover (start_dir)

9.HTMLtestrunner
9.1 introduced HTMLtestrunner
from HTMLTestRunnerNew import HTMLTestrunner
modified portion
with Open ( 'dem.html', 'WB') AS F:
# initialization Runner, logging level of detail, slightly -> fine (0,1 (default), 2)
runner = unittest.TextTestRunner (f, verbosity = 2, title = ' test report name', tester = 'name testers')
# Run
runner.run (Suite)

9.2 Test Report report into a folder
dynamically generating a time file name

Mr. into a report folder
report_dir = os.path.join (start_dir, 'report')
IF not os.path.exists (report_dir):
os.mkdir (report_dir)

# reporting format report / 2019-09-19. html
introduced datatime
from datatime import datatime
time_str = datatime.now () the strftime. ( '% Y-M-% D%% H:% M:% S')
file_name = the os.path.join (report_dir, time_str + 'html')


************************************************************

*** *** test cases must start the Test

********** *****************

'' '


Guess you like

Origin www.cnblogs.com/qzyhome/p/11570199.html