• unittest详解(五) 引入装饰器@classmethod


    我们知道setUp()和setDown()的作用是在每条测试用例执行前准备测试环境以及用例测试结束后恢复测试环境,如果我们执行的测试类下所有测试用例的环境准备和环境复原的操作都是一样的,那么我们就没必要每条测试用例执行前都执行一次setUp()和setDown()的操作。在运行测试类前只执行一次环境的准备,测试类运行结束后只执行一次环境复原的操作,这时我们就可以引入装饰器@classmethod

    代码:

    # coding = utf-8
    import unittest
    import warnings
    from selenium import webdriver
    from time import sleep
    # 驱动文件路径
    driverfile_path = r'D:coshipTest_FrameworkdriversIEDriverServer.exe'
    
    class CmsLoginTest(unittest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            print("test start!")
            # 这行代码的作用是忽略一些告警打印
            warnings.simplefilter("ignore", ResourceWarning)
            cls.driver = webdriver.Ie(executable_path=driverfile_path)
            cls.driver.get("http://172.21.13.83:28080/")
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
            print("test end!")
    
        def test_login1(self):
            '''用户名、密码为空'''
            self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
            self.driver.find_element_by_css_selector("[name='admin.password']").clear()
            self.driver.find_element_by_css_selector("#imageField").click()
            error_message1 = self.driver.find_element_by_css_selector("[for='loginName']").text
            error_message2 = self.driver.find_element_by_css_selector("[for='textfield']").text
            self.assertEqual(error_message1, '用户名不能为空')
            self.assertEqual(error_message2, '密码不能为空')
    
    
        def test_login2(self):
            '''用户名正确,密码错误'''
            self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
            self.driver.find_element_by_css_selector("[name='admin.password']").clear()
            self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
            self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("123456")
            self.driver.find_element_by_css_selector("#imageField").click()
            error_message = self.driver.find_element_by_css_selector(".errorMessage").text
            self.assertEqual(error_message, '密码错误,请重新输入!')
    
        def test_login3(self):
            '''用户名不存在'''
            self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
            self.driver.find_element_by_css_selector("[name='admin.password']").clear()
            self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("test007")
            self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("123456")
            self.driver.find_element_by_css_selector("#imageField").click()
            error_message = self.driver.find_element_by_css_selector(".errorMessage").text
            self.assertEqual(error_message, '用户不存在!')
    
        def test_login4(self):
            '''用户名、密码正确'''
            self.driver.find_element_by_css_selector("[name='admin.loginName']").clear()
            self.driver.find_element_by_css_selector("[name='admin.password']").clear()
            self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
            self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("111111")
            self.driver.find_element_by_css_selector("#imageField").click()
            sleep(1)
            self.driver.switch_to.frame("topFrame")
            username = self.driver.find_element_by_css_selector("#nav_top>ul>li>a").text
            self.assertEqual(username,"autotest")
    
    
    if __name__ == "__main__":
        unittest.main(verbosity=2)

    执行结果:

    "C:Program FilesPython36python.exe" D:/Git/Test_Framework/test_case/test_cmslogin.py
    test start!
    test_login1 (__main__.CmsLoginTest)
    用户名、密码为空 ... ok
    test_login2 (__main__.CmsLoginTest)
    用户名正确,密码错误 ... ok
    test_login3 (__main__.CmsLoginTest)
    用户名不存在 ... ok
    test_login4 (__main__.CmsLoginTest)
    用户名、密码正确 ... ok
    test end!
    
    ----------------------------------------------------------------------
    Ran 4 tests in 22.462s
    
    OK
    
    Process finished with exit code 0

    从执行结果中可以看到,整个测试类的运行过程中,只执行了一次setUpClass()和tearDownClass()

  • 相关阅读:
    unity 颜色空间
    Shader 常用常量 函数等
    Unity3d cg Shader 相关的方法
    DirectX HLSL Shader 内置函数
    unity texture2d 图片尺寸压缩
    Unity中Zxing生成二维码只能生成256大小图片的解决方案
    读取保存 调用系统选框 仅限Win/Mac/WebGL
    Unity编辑器中递归设置文件夹下资源的 AssetBundle Tag 可多选
    扣绿幕Shader(可自选颜色)
    智能手环体验:UP24
  • 原文地址:https://www.cnblogs.com/eastonliu/p/9146279.html
Copyright © 2020-2023  润新知