Selenium2+python automation 52-unittest execution sequence Selenium2+python automation 48-login method (parameterized)

foreword

When many beginners use the unittest framework, they do not know what the execution order of the use cases is. The classes and methods in the test class are not clear, and I don't know when to execute and when not to execute.

This article explains the unittest execution order in detail through the simplest case.

1. Case Analysis

1. First define a test class and write a few simple cases in it

# coding:utf-8
import unittest
import time
class Test(unittest.TestCase):
    def setUp(self):
        print "start!"

    def tearDown(self):
        time.sleep(1)
        print "end!"

    def test01(self ):
        print "Execute test case 01"

    def test03(self):
        print "Execute test case 03"

    def test02(self):
        print "Execute test case 02"

    def addtest(self):
        print "add method"

if __name__ == "__main__":
    unittest.main()

 

2. Execution results

D:\test\python2\python.exe D:/test/test01.py
start!
Execute test case 01
.end!
start!
Execute test case 02
end!
.start!
Execute test case 03
end!
.
----- -------------------------------------------------- ---------------
Ran 3 tests in 3.001s

OK

 

3. Analysis of results

1. Execution order:

start!-Execute test case 01-end!

start!-Execute test case 02-end!

start!-Execute test case 03-end!

2. Several points can be seen from the execution results

--The pre-setUp is executed first, then the use case (test*) is executed, and the post-tearDown is executed last

--The execution order of test cases (test*) is executed according to 01-02-03, that is to say, executed sequentially according to the name of the test case

--addtest(self) This method is not executed, indicating that only the use cases at the beginning of test are executed

 

Four, selenium instance

1. For specific examples, refer to this Selenium2+python automation 48-login method (parameterized)

# coding:utf-8
from selenium import webdriver
import unittest
import time
class Bolg(unittest.TestCase):
    u'''Log in to blog'''
    def setUp(self):
        self.driver = webdriver.Firefox()
        url = "https ://passport.cnblogs.com/user/signin"
        self.driver.get(url)
        self.driver.implicitly_wait(30)

    def login(self, username, psw):
        u'''Here writes a login method , account and password parameterization'''
        self.driver.find_element_by_id("input1").send_keys(username)
        self.driver.find_element_by_id("input2").send_keys(psw)
        self.driver.find_element_by_id("signin"). click()
        time.sleep(3)

    def is_login_sucess(self):
        u'''Determine whether the login account name is obtained'''
        try:
            text = self.driver.find_element_by_id("lnk_current_user").text
            print text
            return True
        except:
            return False

    def test_01(self):
        u'''login case Reference: account, password set by yourself'''
        self.login(u"Shanghai-Yuyou", u"xxxx") # Call the login method
        # Judgment result
        result = self.is_login_sucess()
        self.assertTrue(result)

    def test_02(self ):
        u'''Login case reference: account, password set by yourself'''
        self.login(u"Shanghai-Yuyou", u"xxxx") # Call the login method
        # Judge the result # Exchange QQ group: 232607095
        result = self.is_login_sucess()
        self.assertTrue(result)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339280&siteId=291194637