TA的每日心情 | 无聊 3 天前 |
---|
签到天数: 1050 天 连续签到: 1 天 [LV.10]测试总司令
|
前言
在使用Selenium过程中,目前比较流行的两种框架分别是Unittest和Pytest,之前一直使用Unittest框架,在查阅一些资料的时候发现大部分人在使用Pytest框架,心中不免产生疑惑这两种框架具体区别在什么地方呢,于是花半天时间整理一下具体的区别。
下面以某软件登录功能为例查看一下具体区别:
功能实现用例
源文件:
1.login.py
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium import webdriver
class TestUserLogin(object):
def __init__(self):
self.driver=webdriver.Chrome()
self.driver.get('http://localhost:8888/jpress/user/login')
self.driver.maximize_window()
#登录失败-用户名、密码错误
def test_user_logon_username_error(self):
username=''
pwd='123456'
expected='账号不能为空'
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
#显示等待弹窗出现
WebDriverWait(self.driver,5).until(EC.alert_is_present())
alert=self.driver.switch_to.alert
sleep(3)
assert alert.text==expected
alert.accept()
#self.driver.quit()
#登录成功
def test_user_logon_username_pass(self):
username='wx1'
pwd='123456'
expected='用户中心'
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.title_is(expected))
sleep(3)
assert self.driver.title==expected
self.driver.quit()
|
2.main.py
from test_user_login import *
if __name__ == '__main__':
case=TestUserLogin()
case.test_user_logon_username_error()
case.test_user_logon_username_pass()
|
用例重构
1.unnitest
步骤:
–拷贝之前写好的用例
–更改继承类为unnitest.Testcase
–修改init方法为setup
–assert断言改为unittest断言
–关闭浏览器teardown
注意:清空数据、测试添加序号
代码:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
import unittest
from selenium import webdriver
class TestUserLogin(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver=webdriver.Chrome()
cls.driver.get('http://localhost:8888/jpress/user/login')
cls.driver.maximize_window()
# def __init__(self):
# self.driver=webdriver.Chrome()
# self.driver.get('http://localhost:8888/jpress/user/login')
# self.driver.maximize_window()
#用户名、密码错误
def test_user_logon_username_error(self):
username=''
pwd='123456'
expected='账号不能为空'
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.alert_is_present())
alert=self.driver.switch_to.alert
sleep(3)
#assert alert.text==expected
self.assertEqual(alert.text,expected)
alert.accept()
#self.driver.quit()
def test_user_logon_username_pass(self):
username='wx1'
pwd='123456'
expected='用户中心'
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.title_is(expected))
sleep(3)
assert self.driver.title==expected
self.driver.quit()
if __name__ == '__main__':
unittest.main()
|
2.pytest
重构过程
步骤:
–修改继承类为object
–修改pytest setup方法:setup_class
–修改python断言assert
–使用pytest插件
–pip install pytest-dependency
代码:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium import webdriver
import pytest
class TestUserLogin(object):
def setup_class(self):
self.driver=webdriver.Chrome()
self.driver.get('http://localhost:8888/jpress/user/login')
self.driver.maximize_window()
#用户名、密码错误
def test_user_logon_username_error(self):
username=''
pwd='123456'
expected='账号不能为空'
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.alert_is_present())
alert=self.driver.switch_to.alert
sleep(3)
assert alert.text==expected
alert.accept()
#self.driver.quit()
def test_user_logon_username_pass(self):
username='wx1'
pwd='123456'
expected='用户中心'
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.title_is(expected))
sleep(3)
assert self.driver.title==expected
self.driver.quit()
if __name__ == '__main__':
pytest.main()
|
运行完上面的代码之后我们可以发现,从初级脚本到pytest改写只需要修改pytest setup方法为setup_class(),如果需要函数依赖再安装一个dependency插件即可,较unittest简单。
上面仅仅是跑了两条用例,总结一下具体区别如下表:
|
|