1.unittest Unit Test Framework file structure
UnitTest is one of the Python unit test frameworks that unittest the main file structure of the test framework:
File
>report
>all_case.py
>test_case
>__init__.py
>test_case1 .....
>public
>__init__.py
>login.py
>loginout.py
Test_case folder primarily stores test cases, and test case naming begins with Test_
The public folder is a subfolder of the Test_case folder, primarily for common modules, such as login exit modules
The report folder is primarily stored in test reports that are generated after the test case execution is completed
The All_case script file is the primary execution script that aggregates all the test cases and executes all the test cases
The simplest structure of the 2.unittest frame
Selenium + Python automation Test unittest Framework Learning (a) Selenium principle and application This article says that you can export the recorded browser behavior to the specified programming language, if we first exported the Python script through the Selenium IDE recording, Then you can see that each script has a simple structure that ignores the functions that determine whether an element exists. Its basic structure is as follows:
SetUp ()
TearDown ()
Test_case1 ()
Unittest.main ()
SetUp: initialization function, executed once for each test case execution
TearDown: Cleanup operations such as browser exit after test case execution is complete
Test_case1 (): Written test case script
Unittest.main (): Auto-Execute method with method name beginning with Test_
3.unittest Test framework Optimization Structure 2
A single script cannot contain all of the test cases, so different test cases can be written as different test case scripts placed in the Test_case folder, so when executing the test case we need a test suite testsuite to load all the test case scripts. Add the test Case Method Addtest (), and run the test script with Texttestrunner.
- Construct the test set first
- Instantiating a test suite
- Suite=unittest. TestSuite ()
- Load the test case into the test suite.
- Suite.addtest (Test (' Test_case2 '))
- Suite.addtest (Test (' test_case1 '))
- Executing test Cases
- Instantiate the Texttestrunner class
- Runner=unittest. Texttestrunner ()
- Run the test suite using the Run () method (that is, run all the use cases in the test suite)
The disadvantage of this structure is that each test case adds a addtest () method, which increases the complexity of the automation script, so we can find a way to automatically load eligible test cases
4.unittest Test framework Optimization Structure 3
This structure simplifies some steps, that is, without first creating a test suite and then loading the test case by adding a test case, the Discover method under the Testloader can recursively query the eligible test case loading under the subdirectory under its specified path.
Test_dir = './'
Discover = Unittest.defaultTestLoader.discover (Test_dir, pattern= ' test_*.py ')
Executing test Cases
Instantiate the Texttestrunner class
Runner=unittest. Texttestrunner ()
Run the test suite using the Run () method (that is, run all the use cases in the test suite)
Runner.run (Discover)
5.unittest executes the file all_case.py code structure as follows:
# coding=utf-8import Unittestimport htmltestrunnerimport timefrom test_case.public import logindef createsuit (): #创建测试用 Example Set testunit = UnitTest. TestSuite () #判断是否为测试用例, automatically load the test case into the test suite cslist = "D:\\fcj\\study\\seleniumtest\\test_case" #discover方法定义 discove R = Unittest.defaultTestLoader.discover (#测试用例放置的文件夹名 cslist, pattern = ' test_*.py ', top_level _dir = None) #discover方法筛选出来的用例, Loop added to the test suite for Test_suite in Discover:testunit.addTests (test_suite) Print (testunit) return testunitalltestnames = Createsuit () if __name__ = = "__main__": now = Time.strftime ('%y- %m-%d-%h_%m_%s ', Time.localtime (Time.time ())) #定义报告存放路径 filename = ' d:\\fcj\\study\\seleniumtest\\report\\ ' +now+ ' result.html ' fp = open (filename, ' WB ') #定义测试报告 runner = Htmltestrunner.htmltestrunner (stream = FP, title = U ' A project background test report ', Description = U ' use case execution: ') #执行测试 Runner.run (alltestnames) fp.close ()
Selenium + Python automation Test unittest Framework Learning (II)