美文网首页
43. pytest-fixture

43. pytest-fixture

作者: 薛东弗斯 | 来源:发表于2024-03-20 06:34 被阅读0次
fixture概念fixture是 pytest 用于将测试前后进行预备、清理工作的代码处理机制。

fixture相对于setup和teardown来说有以下几点优势:
fixure命名更加灵活,局限性比较小
conftest.py 配置里面可以实现数据共享,不需要import就能自动找到一些配置
image.png
# 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()

相关文章

  • 你看过的印象深刻或者对你影响深刻的一本书是什么?写出你的收获

    43.

  • pytest-fixture

    作用 1、完成setup和teardown操作,处理数据库、文件等资源的打开和关闭

  • pytest-fixture

    执行测试后自动执行某些操作,用到关键字yeild scope="module"代表在模块的最后一次测试完成后执行s...

  • pytest-fixture用法

    fixture简介 fixture的目的是提供一个固定基线,在该基线上测试可以可靠地和重复地执行。fixture提...

  • Multiply Strings And Add Strings

    43. Multiply Strings 415. Add Strings

  • Stray Birds

    43. The fish in the water is silent, the animal on the ea...

  • pytest-fixture的使用

    http://doc.pytest.org/en/latest/fixture.html fixture的优点 显...

  • pytest-fixture的使用

    fixture可以让我们自定义测试用例的前置条件 fixture实现teardown后置条件操作

  • 43.

    顾十四月时不知道从哪里弄来了两只仓鼠,说是养小动物可以缓解寂寞空虚。然而今天早起起床,却发现笼子里多了三只红色没毛...

  • 43.

    我读到的: 1.我们阅读一篇故事的时候,我们的大脑会重新激活以前经历留下的痕迹,从而赋予页面上的词语意义。 2.你...

网友评论

      本文标题:43. pytest-fixture

      本文链接:https://www.haomeiwen.com/subject/unlnzdtx.html