pytest-assume, 即使case执行fail也会执行后续的测试用例。
# pip install pytest-assume
import pytest
class TestCase_1:
def test_1(self):
name = 123
assert name == 123
def test_2(self):
assert 1 == 1
assert 1 != 2
assert 3 > 1
assert 1 < 3
assert 3 >= 3
assert 3 <= 3
assert "a" in "abc"
pytest.assume("d" in "abc")
pytest.assume("d" not in "abc")
assert True is True
assert False is not True
if __name__=="__main__":
pytest.main()
pytest + selenimu测试百度网页
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_baidu():
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下1"
创建pytest配置项 pytest.ini 配置-指定运行目录或运行文件
python project里面新建一个pytest.ini, 普通文件
[pytest]
testpaths = ./testcases
# 其中 ./代表是当前项目文件见
testcases代表当前项目下面的一个测试用例文件夹。
# 有了pytest.ini,pytest执行脚本默认就会从projet/testcases文件夹里面去找执行文件。如不不这么配置,则pytest会从整个project里面遍历所有的py文件。
![](https://img.haomeiwen.com/i3968643/8ae20ff1dda1e103.png)
常用运行参数
![](https://img.haomeiwen.com/i3968643/7cf2fc5b67153902.png)
-m markes标记
# pytest.ini
[pytest]
;使用testpaths指定测试用例运行目录或者运行文件
testpaths = ./testcases
markers=
pro:pro
test:test
# project > testcases>test_1.py
import pytest
@pytest.mark.pro # 给测试用例打标签,pro
def test_1():
name = 123
assert name == 1234
@pytest.mark.test # 给测试用例打标签,test
def test_2():
num = 20
assert num == 20
Terminal里面,pytest -m pro, 则只执行标签为pro的测试用例
pytest -m test, 则只执行标签为test的测试用例
-k 关键字标记
pytest -k "关键词" 则可以执行测试用例
关键词:可以是测试用例名关键词,也可以是.py文件名的关键词
-q 简化控制台输出,只有结果。默认参数
-v 输出详细的执行信息
-s 输出调试信息
# pytest.ini
[pytest]
;使用testpaths指定测试用例运行目录或者运行文件
testpaths = ./testcases
markers=
pro:pro
test:test
addopts: -v
指定执行顺序 pytest-ordering
pip install pytest-ordering
通过 @pytest.mark.run(order=XXX) 来执行执行顺序
import pytest
class TestUser:
@pytest.mark.run(order=2)
def test_login(self):
print("logging……")
assert 1 == 1
@pytest.mark.run(order=1)
def test_register(self):
print("register...")
assert 0 == 0
前置/后置 setup/teardown
![](https://img.haomeiwen.com/i3968643/99f27036c3426e23.png)
# test_module.py
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
def setup_module():
global driver
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器,并最大化")
def teardown_module():
driver.quit()
driver.close()
print("关闭浏览器")
class TestBaidu:
def test_baidu(self):
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
def test_baidu2(self):
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
if __name__=="__main__":
pytest.main()
test_function
# test_funciton.py
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
def setup_funciton():
global driver # 此处的global变量无法传入class里面。因为是function级别的定义,而不是module级别
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器,并最大化")
def teardown_funciton():
driver.quit()
driver.close()
print("关闭浏览器")
def test_baidu():
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
def test_baidu2():
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
if __name__=="__main__":
pytest.main()
test_class
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestBaidu:
def setup_class(self):
global driver
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器,并最大化")
def teardown_class(self):
driver.quit()
driver.close()
print("关闭浏览器")
def test_baidu(self):
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
def test_baidu2(self):
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
if __name__=="__main__":
pytest.main()
test_method
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestBaidu:
def setup_method(self):
global driver
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器,并最大化")
def teardown_method(self):
driver.close()
driver.quit()
print("关闭浏览器")
def test_baidu(self):
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
def test_baidu1(self):
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]').text
button_text = driver.find_element(By.ID, 'su').accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下1"
if __name__ == '__main__':
pytest.main()
setup、teardown不常用。一般用fixture去实现前置、后置动作。
测试用例跳过
![](https://img.haomeiwen.com/i3968643/56d56aa5020e52fc.png)
import pytest
a = 1
# 强制跳过
@pytest.mark.skip(reason="我不想执行")
def test_skip1():
assert 1 == 1
# 条件跳过
@pytest.mark.skipif(a == 2, reason="判断1等于1")
def test_skip2():
assert 1 == 1
def test_skip3():
if a > 1:
pytest.skip("测试用例中跳过")
assert 1 == 1
if __name__ == '__main__':
pytest.main()
网友评论