Selenium2+python automation 55-unittest decorator (@classmethod) Selenium2+python automation 52-unittest execution order

foreword

As mentioned earlier, setUp in unittest can be executed before each use case is executed, which effectively reduces the amount of code, but there is a drawback, such as opening the browser operation, it will be reopened every time the use case is executed, which will waste a lot of time.

So I wonder if it is possible to open the browser only once, and then close it after executing the use case? This requires the use of decorators (@classmethod) to solve.

 

1. Decorator

1. The difference between setUp and setUpClass

setup(): run
teardown() before each test case is run:
setUpClass() is executed after each test case is run: @classmethod decorator must be used, and
tearDownClass() is run only once before all cases run: @classmethod must be used to decorate , run only once after all cases are run

2. @ is a modifier, classmethod is a class method in python

 

2. Execution order

1. Write a few simple cases with class methods, you can compare this: Selenium2+python automation 52-unittest execution order

# coding:utf-8
import unittest
import time
class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print "start!"
   

    @classmethod
    def tearDownClass(cls):
        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. It can be seen from the execution results that the pre and post are executed only once before the use case is executed.

start!
Execute test case 01
Execute test case 02
Execute test case 03
...end!

------------------------------- ---------------------------------------
Ran 3 tests in 1.001s

 

Three, selenium instance

1. You can put the operation of opening the browser into the pre-setUpClass(cls), so that you can open the browser once and execute multiple cases.

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import unittest
class BolgHome(unittest.TestCase):
    u'''blog homepage'''
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        url = "http://www.cnblogs.com/yoyoketang/"
        cls.driver.get(url)
        cls.driver.implicitly_wait(30)

    @classmethod
    def tearDownClass(cls):
        cls.driver. quit()

    def test_01(self):
        u'''Verify that the element exists: Blog Garden'''
        locator = ("id", "blog_nav_sitehome")
        text = u"Blog Garden"
        result = EC.text_to_be_present_in_element(locator, text)(self.driver)
        self.assertTrue(result)

    def test_02(self):
        u'''验证元素存在:首页'''
        locator = ("id", "blog_nav_myhome")
        text = u"首页"
        result = EC.text_to_be_present_in_element(locator, text)(self.driver)
        self.assertTrue(result)

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

Guess you like

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