自动化测试用例
在自动化测试里面,不能对环境造成任何的破坏,也就是说,执行前和执行后,环境都是一样的 ,不能因为执行了自动化测试的case,导致产生了垃圾数据。
setUp()——>初始化
tearDown——>清理
测试方法——测试步骤
断言
unittest测试
测试用例注意事项如下:
1、在一个测试类里,每一个测试方法都以test开头的,test不能是中间或者尾部,建议test_
2、每一个测试用例方法都应该有注释信息,这样在测试报告就会显示具体的测试点的检查点
3、在自动化测试中,每个测试用例都必须得有断言,无断言的自动化测试用例是无效的
4、最好一个测试用例方法对应一个业务测试点,不要多个业务检查点写一个测试用例
5、如果涉及到业务逻辑的处理,最好把业务逻辑的处理方法放在断言前面,这样做的目的是不要因为业务逻辑执行错误导致断言也是失败
6、测试用例名称最好规范,有约束
7、是否先写自动化测试的测试代码,在使用自动化测试方式写,本人觉得没必要,毕竟能够做自动化测试的都具备了功能测试的基本水平,所以没必要把一个业务的检查点写多次,浪费时间和人力成本
# !/usr/bin/env python # -*- coding:utf-8 -*- 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_xpath('//*[@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('admin@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, '请输入密码') def test_sina_004(self): nowHander=self.driver.current_window_handle self.driver.find_element_by_link_text('注册') allHander=self.driver.current_window_handle for hander in allHander: if nowHander!=hander: self.driver.switch_to.window(hander) self.assertEqual(self.driver.current_url,'https://mail.sina.com.cn/register/regmail.php') if __name__ == '__main__': unittest.main(verbosity=2)
# 执行一次 import unittest class TestUi(unittest.TestCase): @classmethod def setUpClass(cls) -> None: print('开始执行') def tearDownClass(cls) -> None: print('执行结束') def test_login_001(self): pass def test_login_002(self): pass if __name__ == '__main__': unittest.main()
# !/usr/bin/env python # -*- coding:utf-8 -*- # 执行多次 import unittest from selenium import webdriver class ApiTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('http://www.baidu.com') self.driver.implicitly_wait(30) def tearDown(self): self.driver.quit() def test_baidu_title(self): self.assertEqual(self.driver.title, '百度一下,你就知道') def test_baidu_url(self): self.assertEqual(self.driver.current_url, 'https://www.baidu.com/') if __name__ == '__main__': unittest.main(verbosity=2)
# !/usr/bin/env python # -*- coding:utf-8 -*- # 测试分离,使用继承 from selenium import webdriver import unittest import time as t class Init(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('https://mail.sina.com.cn') self.driver.implicitly_wait(30) def tearDown(self): self.driver.quit() class TestSina(Init): def test_sina_001(self): isLogin = self.driver.find_element_by_xpath('//*[@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('admin@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, '请输入密码') def test_sina_004(self): nowHander=self.driver.current_window_handle self.driver.find_element_by_link_text('注册') allHander=self.driver.window_handles for hander in allHander: if nowHander!=hander: self.driver.switch_to.window(hander) self.assertEqual(self.driver.current_url,'https://mail.sina.com.cn/register/regmail.php') if __name__ == '__main__': unittest.main(verbosity=2)
# !/usr/bin/env python # -*- coding:utf-8 -*- # params()参数传递 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, 2, 3), param(1.0, 2.0, 3.0), param('hi', ' admin', 'hi admin') ]) def test_add(self, a, b, result): self.assertEqual(add(a, b), result) if __name__ == '__main__': unittest.main(verbosity=2)
参数化前提
安装对应库:pip3 install parameterized
相同的步骤
不同的测试场景
# !/usr/bin/env python # -*- coding:utf-8 -*- 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('adminadmin@sina.com', 'admin', '登录名或密码错误'), param('admin', ' admin', '您输入的邮箱名格式不正确') ]) def test_sina_login(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(1) 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) ''' # qq邮箱测试参数传递 class UITest(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('https://mail.qq.com/') self.driver.implicitly_wait(30) def tearDown(self) -> None: self.driver.quit() @parameterized.expand([ param('', '', '你还没有输入帐号!'), param('账号@qq.com', 'admin', '你输入的帐号或密码不正确,请重新输入。'), param('账号@qq.com', ' admin', '你输入的帐号或密码不正确,请重新输入。') ]) def test_sina_login(self, username, password, divText): self.driver.switch_to.frame('login_frame') self.driver.find_element_by_name('u').send_keys(username) self.driver.find_element_by_name('p').send_keys(password) t.sleep(1) self.driver.find_element_by_id('login_button').click() t.sleep(1) errorText = self.driver.find_element_by_xpath('//*[@id="err_m"]') self.assertEqual(errorText.text, divText) if __name__ == '__main__': unittest.main(verbosity=2)