43. pytest-fixture
2024-03-20 本文已影响0人
薛东弗斯
fixture概念fixture是 pytest 用于将测试前后进行预备、清理工作的代码处理机制。
fixture相对于setup和teardown来说有以下几点优势:
fixure命名更加灵活,局限性比较小
conftest.py 配置里面可以实现数据共享,不需要import就能自动找到一些配置

# pytest.ini
[pytest]
;使用testpaths指定测试用例运行目录或者运行文件
testpaths = ./CH0/
;markers=
; pro:pro
; test:test
addopts: -vs
autouse=True: 强制每个函数都会调用该fixture。 不设定,则默认 autouse=False
import pytest
@pytest.fixture(scope="function",autouse=True)
def fixture1():
print("我是前置步骤1。。。")
return 1
@pytest.fixture(scope="function")
def fixture2():
print("我是前置步骤2。。。")
return 1
def test_fixture1():
assert 1 == 1
def test_fixture2(fixture2):
assert 1 == 1
if __name__ == '__main__':
pytest.main()
利用fixture执行代码中公共的部分,减少重复代码
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
@pytest.fixture()
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
return driver
class TestBaidu:
def test_baidu(self, driver):
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):
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()
scope = class,则每个类调用一次。对于该案例,只打开一次浏览器. class是我们更希望用的
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
@pytest.fixture(scope="class")
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器")
return driver
class TestBaidu:
def test_baidu(self, driver):
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):
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()
scope=module,当前测试用例效果与scope=class同
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
@pytest.fixture(scope="module")
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器")
return driver
class TestBaidu:
def test_baidu(self, driver):
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):
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()
scope=session
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
@pytest.fixture(scope="session")
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器")
return driver
class TestBaidu:
def test_baidu(self, driver):
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):
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()