1, Unittest.main ()
Turning a unit test module into a test script that can be run directly, the main () method uses the Testloader class to search for all test methods contained in the module that begin with the name "Test" and automatically executes them. The default order of execution methods is to load test cases according to the Order of ASCII code, in the order of numbers and letters: 0-9,a-z,a-z. So the test case method that starts with a will take precedence and will be executed after the start of a.
Class Test_tc_login (UnitTest. TestCase):
def setUp (self):
Self.driver = Webdriver. Chrome (Cc.driverpath ())
Self.base_url = Cc.baseurl ()
Self.testcaseinfo = Testcaseinfo (id=1, name= "Test case name", owner= ' Xua ')
Self.testresult = Testreport ()
Logutility.createloggerfile ("Test_tc_login")
def test_a (self):
Try
Self.testCaseInfo.starttime = Cc.getcurrenttime ()
# Step1:open Base Site
LogUtility.Log ("Open Base site" + self.base_url)
Self.driver.get (Self.base_url)
# Step2:open Login Page
Login_page = LoginPage (self.driver)
# Step3:enter Username & password
LogUtility.Log ("Login Web using username")
Login_page.set_username ("username")
Login_page.set_password ("password")
Time.sleep (2)
# Checkpoint1:check Popup Dialog title
LogUtility.Log ("Check whether sign in dialog exists or not")
Self.assertequal (Login_page.get_diaglogtitle (), "Sign in")
# Time.sleep (3)
# Step4:cancel Dialog
Login_page.click_cancel ()
Self.testCaseInfo.result = "Pass"
Except Exception as err:
Self.testCaseInfo.errorinfo = str (ERR)
LogUtility.Log ("Got error:" + str (ERR))
Finally
Self.testCaseInfo.endtime = Cc.getcurrenttime ()
Self.testCaseInfo.secondsDuration = Cc.timediff (Self.testCaseInfo.starttime, Self.testCaseInfo.endtime)
def tearDown (self):
Self.driver.close ()
Self.testResult.WriteHTML (Self.testcaseinfo)
if __name__ = = ' __main__ ':
Unittest.main ()
2. Batch execution: Suite.addtest
If __name__== "__main__":
Suite = UnitTest. TestSuite ()
Suite.addtest (Testindex ("Test_index")) #类里的某个测试方法
Suite.addtest (Testsys (' TEST_DDT '))
Runner=unittest. Texttestrunner ()
Runner.run (Suite)
This allows you to execute all the use cases in a file, or use "#" to comment out the use case that you do not currently need to execute.
3, Unittest.defaultTestLoader.discover
The Defaulttestloader () class, through the Discover () method under the class, can automatically test the catalog Start_dir match find test Case files (test*.py) and assemble the found test case into a test suite, so you can go directly through the run () Method to execute the Discover. Use the following:
Discover=unittest.defaulttestloader.discover (Test_dir, pattern= ' test_*.py ')
if __name__ = = "__main__":
# Set report File Save path
Report_path = OS.GETCWD () + '/test_report/'
# Get System Current time
now = Time.strftime ("%y-%m-%d-%h_%m_%s", Time.localtime (Time.time ()))
# Set Report name format
Htmlfile = Report_path + now+ "automated test report. HTML"
fp = open (Htmlfile, "WB")
SuiteA = UnitTest. Testloader (). Loadtestsfromtestcase (Testsys)
Suiteb = UnitTest. Testloader (). Loadtestsfromtestcase (Testauth)
Suite = UnitTest. TestSuite ([SuiteA, Suiteb])
Runner = Htmltestrunner.htmltestrunner (STREAM=FP, title=u "Merchant Management System Test Report", Description=u "use case test Case")
#runner = UnitTest. Texttestrunner ()
Runner.run (Suite)
Python+selenium organization use Case Method summary