💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 测试用例优化 * [引入单元测试框架](http://brtc.imsam.cn/1380706#_95) * [封装公共部分](http://brtc.imsam.cn/1380706#_198) * [使用setUp和tearDown优化](http://brtc.imsam.cn/1380706#setUptearDown_236) * [断言](http://brtc.imsam.cn/1380706#_273) * [封装到public中](http://brtc.imsam.cn/1380706#public_342) 之前我们所写的是线性代码 ~~~ #coding=utf-8 # @Author: SamWang # @Date: 2019-11-15 16:33:56 # @Last Modified time: 2019-11-16 10:42:48 from selenium import webdriver from time import sleep # 登录成功 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('test') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('test123') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#wp-admin-bar-my-account').text if 'test' in login_msg: print('登录成功用例:pass') else: print('登录成功用例:fail') d.close() d.quit() # 用户名为空密码正确填写,登录失败 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('test123') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#login_error').text if '用户名一栏为空' in login_msg: print('用户名为空密码正确填写,登录失败:pass') else: print('用户名为空密码正确填写,登录失败:fail') d.close() d.quit() # 密码为空用户名正确填写,登录失败 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('test') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#login_error').text if '密码一栏为空' in login_msg: print('密码为空用户名正确填写,登录失败:pass') else: print('密码为空用户名正确填写,登录失败:fail') d.close() d.quit() # 用户名正确密码错误时,登录失败 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('test') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('tes222t123') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#login_error').text if '密码不正确' in login_msg: print('用户名正确密码错误时,登录失败:pass') else: print('用户名正确密码错误时,登录失败:fail') d.close() d.quit() ~~~ > ### 引入单元测试框架 ~~~ #coding=utf-8 # @Author: SamWang # @Date: 2019-11-15 16:33:56 # @Last Modified time: 2019-11-16 10:45:53 from selenium import webdriver from time import sleep import unittest class mytest(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_login_success(self): # 登录成功 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('test') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('test123') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#wp-admin-bar-my-account').text if 'test' in login_msg: print('登录成功用例:pass') else: print('登录成功用例:fail') d.close() d.quit() def test_login_username_empty(self): # 用户名为空密码正确填写,登录失败 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('test123') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#login_error').text if '用户名一栏为空' in login_msg: print('用户名为空密码正确填写,登录失败:pass') else: print('用户名为空密码正确填写,登录失败:fail') d.close() d.quit() def test_login_password_empty(self): # 密码为空用户名正确填写,登录失败 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('test') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#login_error').text if '密码一栏为空' in login_msg: print('密码为空用户名正确填写,登录失败:pass') else: print('密码为空用户名正确填写,登录失败:fail') d.close() d.quit() def test_login_password_error(self): # 用户名正确密码错误时,登录失败 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys('test') sleep(1) d.find_element_by_css_selector('#user_pass').send_keys('tes222t123') sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) login_msg = d.find_element_by_css_selector('#login_error').text if '密码不正确' in login_msg: print('用户名正确密码错误时,登录失败:pass') else: print('用户名正确密码错误时,登录失败:fail') d.close() d.quit() if __name__ == '__main__': unittest.main() ~~~ > ### 封装公共部分 ~~~ from selenium import webdriver from time import sleep import unittest def login_action(d,username,password): d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys(username) sleep(1) d.find_element_by_css_selector('#user_pass').send_keys(password) sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) class mytest(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_login_success(self): # 登录成功 d = webdriver.Chrome() d.maximize_window() d.implicitly_wait(10) login_action(d=d,username='test',password='test123')# 调用登录函数 login_msg = d.find_element_by_css_selector('#wp-admin-bar-my-account').text if 'test' in login_msg: print('登录成功用例:pass') else: print('登录成功用例:fail') d.close() d.quit() ~~~ > ### 使用setUp和tearDown优化 ~~~ from selenium import webdriver from time import sleep import unittest def login_action(d,username,password): d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys(username) sleep(1) d.find_element_by_css_selector('#user_pass').send_keys(password) sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) #同一个类下,如果该方法的变量想要能够被其他方法所使用,则在定义变量时,加上一个self. class mytest(unittest.TestCase): def setUp(self): self.d = webdriver.Chrome() self.d.maximize_window() self.d.implicitly_wait(10) def tearDown(self): self.d.close() self.d.quit() def test_login_success(self): # 登录成功 login_action(d=self.d,username='test',password='test123')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#wp-admin-bar-my-account').text if 'test' in login_msg: print('登录成功用例:pass') else: print('登录成功用例:fail') ~~~ > ### 断言 断言 (判断实际结果与预期结果是否相符) | 断言方式 | 作用 | | --- | --- | | self.assertIn(a,b) | 判断 a 是不是 in b | | self.assertNotIn(a,b) | 判断 a 是不是 not in b | | self.assertEqual(a,b) | 判断 a 是不是 等于 b | | self.assertNotEqual(a,b) | 判断 a 是不是 不等于 b | ~~~ from selenium import webdriver from time import sleep import unittest def login_action(d,username,password): d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys(username) sleep(1) d.find_element_by_css_selector('#user_pass').send_keys(password) sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) #同一个类下,如果该方法的变量想要能够被其他方法所使用,则在定义变量时,加上一个self. class mytest(unittest.TestCase): def setUp(self): self.d = webdriver.Chrome() self.d.maximize_window() self.d.implicitly_wait(10) def tearDown(self): self.d.close() self.d.quit() def test_login_success(self): # 登录成功 login_action(d=self.d,username='test',password='test123')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#wp-admin-bar-my-account').text self.assertIn('test',login_msg) def test_login_username_empty(self): #用户名为空密码正确填写,登录失败 login_action(d=self.d,username='',password='test123')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#login_error').text self.assertIn('用户名一栏为空',login_msg) def test_login_password_empty(self): # 密码为空 login_action(d=self.d,username='test',password='')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#login_error').text self.assertIn('密码一栏为空',login_msg) def test_login_password_error(self): # 用户名正确密码错误时,登录失败 login_action(d=self.d,username='test',password='te124st123')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#login_error').text self.assertIn('密码不正确',login_msg) if __name__ == '__main__': unittest.main() ~~~ > ### 封装到public中 如果当前脚本的函数,需要被其他脚本所引用,那么我们就需要创建一个public来存放这些共用的函数.模块 比如将登陆的login\_action函数封装到public中 1、在testcase文件夹下创建一个`public.py`的文件 ![](https://img.kancloud.cn/37/f8/37f80e9d5823a6f361ca1ab2fb06d538_628x361.png) 2、将login\_case.py中的登录函数移动到public.py上 ![](https://img.kancloud.cn/ee/19/ee190de6af19282256a3a060923c23a5_1038x369.png) 3、 修改login\_case.py的调用方式 ![](https://img.kancloud.cn/e8/18/e81854545a943151e8caf0b7c12ee0c6_764x241.png) 修改后的`login_case.py`代码如下 ~~~ from selenium import webdriver from time import sleep import unittest from public import login_action #同一个类下,如果该方法的变量想要能够被其他方法所使用,则在定义变量时,加上一个self. class mytest(unittest.TestCase): def setUp(self): self.d = webdriver.Chrome() self.d.maximize_window() self.d.implicitly_wait(10) def tearDown(self): self.d.close() self.d.quit() def test_login_success(self): # 登录成功 login_action(d=self.d,username='test',password='test123')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#wp-admin-bar-my-account').text self.assertIn('test',login_msg) def test_login_username_empty(self): #用户名为空密码正确填写,登录失败 login_action(d=self.d,username='',password='test123')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#login_error').text self.assertIn('用户名一栏为空',login_msg) def test_login_password_empty(self): # 密码为空 login_action(d=self.d,username='test',password='')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#login_error').text self.assertIn('密码一栏为空',login_msg) def test_login_password_error(self): # 用户名正确密码错误时,登录失败 login_action(d=self.d,username='test',password='te124st123')# 调用登录函数 login_msg = self.d.find_element_by_css_selector('#login_error').text self.assertIn('密码不正确',login_msg) if __name__ == '__main__': unittest.main() ~~~ 修改后的`public.py`代码如下 ~~~ from time import sleep def login_action(d,username,password): d.get('http://os-201804081018/blog/wp-login.php') d.find_element_by_css_selector('#user_login').send_keys(username) sleep(1) d.find_element_by_css_selector('#user_pass').send_keys(password) sleep(1) d.find_element_by_css_selector('#wp-submit').click() sleep(1) ~~~