本文采用pytest框架运行了两个测试用例。
与unittest框架相比:
- 引入包不同
- pytest的类不需要继承类
- 测试固件的方法不同,pyest中setup和teardown是小写。
- 断言方法不同 pytest直接用assert断言
import pytest
from selenium import webdriver
class TestStorm():
def setup(self):
self.driver=webdriver.Chrome()
self.driver.get("http://demo.redmine.org/")
self.driver.maximize_window()
self.driver.implicitly_wait(30)
@pytest.mark.L2
def test_wrongpw(self):
""" 正确的用户名错误的密码"""
self.driver.find_element_by_xpath('//*[@id="account"]/ul/li[1]/a').click()
self.driver.find_element_by_id('username').clear()
self.driver.find_element_by_id('username').send_keys('11111111111111')
self.driver.find_element_by_id('password').clear()
self.driver.find_element_by_id('password').send_keys('1111111111111')
self.driver.find_element_by_name('login').click()
errormessage=self.driver.find_element_by_xpath('//*[@id="flash_error"]').text
# self.assertEqual(errormessage,'无效的用户名或密码')
assert errormessage=='无效的用户名或密码'
@pytest.mark.L1
@pytest.mark.L2
def test_successlogin(self):
""" 正确的用户名正确的密码"""
self.driver.find_element_by_xpath('//*[@id="account"]/ul/li[1]/a').click()
self.driver.find_element_by_id('username').clear()
self.driver.find_element_by_id('username').send_keys('111111111')
self.driver.find_element_by_id('password').clear()
self.driver.find_element_by_id('password').send_keys('111111111111')
self.driver.find_element_by_name('login').click()
idaccount=self.driver.find_element_by_xpath('//*[@id="loggedas"]/a').text
#self.assertEqual(idaccount,'1111111111')
assert idaccount=='yangyali'
def teardown(self):
self.driver.quit()
if __name__=='__main__':
pytest.main(["-s","test_login_py.py","--html=./report0105.html"])
生成测试报告如下:
控制台输出:
D:\软件\python.exe D:/WebUI/venv/Scripts/assets/test_login_py.py
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.3, py-1.8.1, pluggy-0.13.1
rootdir: D:\WebUI\venv\Scripts\assets
plugins: allure-pytest-2.9.45, html-3.1.1, metadata-1.11.0
collected 2 items
test_login_py.py ..
============================== warnings summary ===============================
test_login_py.py:10
D:\WebUI\venv\Scripts\assets\test_login_py.py:10: PytestUnknownMarkWarning: Unknown pytest.mark.L2 - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
@pytest.mark.L2
test_login_py.py:24
D:\WebUI\venv\Scripts\assets\test_login_py.py:24: PytestUnknownMarkWarning: Unknown pytest.mark.L1 - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
@pytest.mark.L1
test_login_py.py:25
D:\WebUI\venv\Scripts\assets\test_login_py.py:25: PytestUnknownMarkWarning: Unknown pytest.mark.L2 - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
@pytest.mark.L2
-- Docs: https://docs.pytest.org/en/latest/warnings.html
-- generated html file: file://D:\WebUI\venv\Scripts\assets\report0105.html ---
======================= 2 passed, 3 warnings in 27.13s ========================
Process finished with exit code 0
?
|