UnitTest测试固件、参数parameterized

 

1、UnitTest的使用:

   (1)、setUp()、teardown()每个测试用例都执行一次

import unittest
from selenium import webdriver

class TestUi(unittest.TestCase):
    def setUp(self) -> None:
        print('开始执行')

    def  tearDown(self) -> None:
        print('执行结束')

    def test_login_001(self):
        pass

    def test_login_002(self):
        pass

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

  例:

from selenium import  webdriver
import  unittest
import time as t

class TestSina(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        '''初始化'''
        cls.driver=webdriver.Chrome()
        cls.driver.maximize_window()
        cls.driver.get('https://mail.sina.com.cn/')
        cls.driver.implicitly_wait(30)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()

    def test_sina_001(self):
        isLogin=self.driver.find_element_by_id('store1')
        #验证
        self.assertEqual(isLogin.is_selected(),True)

    def test_sina_002(self):
        self.driver.find_element_by_link_text('登录').click()
        t.sleep(3)
        #注意定位,容易出错
        divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
        self.assertEqual(divText.text,'请输入邮箱名')

    def test_sina_003(self):
        self.driver.find_element_by_id('freename').send_keys('lyl123@sina.com')
        self.driver.find_element_by_link_text('登录').click()
        t.sleep(3)
        divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[2]')
        self.assertEqual(divText.text=='请输入密码',True)

    def test_sina_004(self):
            # 先获取注册页面的窗口
            nowHander = self.driver.current_window_handle
            # 点击注册页面,跳转到注册的页面
            self.driver.find_element_by_link_text('注册').click()
            # 获取所有窗口
            allHanders = self.driver.window_handles
            # 对所有的窗口进行循环处理
            for handler in allHanders:
                # 判断当前窗口(如果不是当前窗口)
                if nowHander != handler:
                    # 获取当前窗口
                    self.driver.switch_to.window(handler)
                    # 断言
                    self.assertEqual(self.driver.current_url, 'https://mail.sina.com.cn/register/regmail.php')
                    self.driver.close()

if __name__=='__main__':
    unittest.main(verbosity=2)

 (2)、setUpClass()、teardownClass()只执行一次

import unittest
from selenium import webdriver

class TestUi(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print('开始执行')

    @classmethod
    def  tearDownClass(cls) -> None:
        print('执行结束')

    def test_login_001(self):
        pass

    def test_login_002(self):
        pass

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

  例:

from selenium import  webdriver
import  unittest
import time as t

class TestSina(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        '''初始化'''
        cls.driver=webdriver.Chrome()
        cls.driver.maximize_window()
        cls.driver.get('https://mail.sina.com.cn/')
        cls.driver.implicitly_wait(30)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()

    def test_sina_001(self):
        isLogin=self.driver.find_element_by_id('store1')
        #验证
        self.assertEqual(isLogin.is_selected(),True)

    def test_sina_002(self):
        self.driver.find_element_by_link_text('登录').click()
        t.sleep(3)
        #注意定位,容易出错
        divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
        self.assertEqual(divText.text,'请输入邮箱名')

    def test_sina_003(self):
        #先获取注册页面的窗口
        nowHander=self.driver.current_window_handle
        #点击注册页面,跳转到注册的页面
        self.driver.find_element_by_link_text('注册').click()
        #获取所有窗口
        allHanders=self.driver.window_handles
        #对所有的窗口进行循环处理
        for handler in allHanders:
            #判断当前窗口(如果不是当前窗口)
            if nowHander!= handler:
                #获取当前窗口
                self.driver.switch_to.window(handler)
                #断言
                self.assertEqual(self.driver.current_url,'https://mail.sina.com.cn/register/regmail.php')
                self.driver.close()
                #关闭注册页面后在返回到登录页面(才能继续执行下一步)
                self.driver.switch_to.window(nowHander)

    def test_sina_004(self):
        self.driver.find_element_by_id('freename').send_keys('lyl123@sina.com')
        self.driver.find_element_by_link_text('登录').click()
        t.sleep(3)
        divText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[2]')
        self.assertEqual(divText.text=='请输入密码',True)

if __name__=='__main__':
    unittest.main(verbosity=2)

2、安装parameterized(参数化)

   在电脑搜索栏中输入cmd(打开命令提示符),在管理员权限下--->输入pip3 install parameterized(自动安装)

 

3、param使用

import unittest
from parameterized import  parameterized,param

def add(a,b):
    return a+b

class AddTest(unittest.TestCase):
    def setUp(self) -> None:
        pass

    def tearDown(self) -> None:
        pass

    @parameterized.expand([
        param(1,1,2),
        param(2,2,4),
        param(1.0,2.0,3.0),
        param('ni',' hao','ni hao'),
    ])
    def test_add(self,a,b,result):
        self.assertEqual(add(a,b),result)

if __name__ == '__main__':
    unittest.main(verbosity=2) 

 (1)、 Sina email()实战

import unittest
from parameterized import  parameterized,param
from selenium import webdriver
import time as t

class UITest(unittest.TestCase):
    def setUp(self) -> None:
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get('https://mail.sina.com.cn/')
        self.driver.implicitly_wait(30)

    #清理(退出)
    def tearDown(self) -> None:
        self.driver.quit()

    @parameterized.expand([
        param('','','请输入邮箱名'),
        param('lyl2580@sina.com',123,'登录名或密码错误'),
        param('lyl',' 369','您输入的邮箱名格式不正确'),
    ])
    def test_sina(self,username,password,divText):
        self.driver.find_element_by_id('freename').send_keys(username)
        self.driver.find_element_by_id('freepassword').send_keys(password)
        self.driver.find_element_by_link_text('登录').click()
        t.sleep(3)
        errorText=self.driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
        self.assertEqual(errorText.text,divText)

if __name__ == '__main__':
    unittest.main(verbosity=2)

 (2)、网易email(实战) 

import unittest
from parameterized import  parameterized,param
from selenium import webdriver
import time as t

class UITest(unittest.TestCase):
    def setUp(self) -> None:
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get('https://mail.163.com/')
        self.driver.implicitly_wait(30)
        self.driver.switch_to.frame(0)

    #清理(退出)
    def tearDown(self) -> None:
        self.driver.quit()

    @parameterized.expand([
        param('','','请输入帐号'),
        param('lyl2580','','请输入密码'),
        param('1542#$^%&^','15263','帐号格式错误'),
        param('lyl@163.com',' 369','请先进行验证'),
    ])
    def test_wangyi(self,username,password,divText):
        self.driver.find_element_by_name('email').send_keys(username)
        self.driver.find_element_by_name('password').send_keys(password)
        self.driver.find_element_by_link_text('登  录').click()
        t.sleep(3)
        errorText=self.driver.find_element_by_xpath('//*[@id="nerror"]')
        self.assertEqual(errorText.text,divText)

if __name__ == '__main__':
    unittest.main(verbosity=2)

  

 

 

 

posted @ 2021-08-18 18:53  就不将就就  阅读(218)  评论(0编辑  收藏  举报