当前位置: 首页 > 知识库问答 >
问题:

在订购参数化测试时,使用类范围访问pytest夹具参数

吕天逸
2023-03-14

    import pytest
    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    from webdriver_manager.firefox import GeckoDriverManager
    
    @pytest.fixture(params=["Chrome","Firefox"],scope='class')
    def oneTimeSetup1(request):
        if request.param == "Chrome":
            driver = webdriver.Chrome(ChromeDriverManager().install())
        if request.param == "Firefox":
            driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
    
        driver.implicitly_wait(5)
        driver.maximize_window()
        driver.get("https://courses.letskodeit.com/practice")
    
        if request.cls is not None:
            request.cls.driver = driver
        print("the velue of param is " + request.param)

    
        yield driver
        driver.quit()

我的测试结构是

dir tests
--conftest.py
--test_one.py
----TestClassOne
------test_one
------test_two

当我收集测试时,我可以看到下面

<Package tests>
  <Module test_one.py>
    <Class TestClassOne>
        <Function test_one[Chrome]>
        <Function test_one[Firefox]>
        <Function test_two[Chrome]>
        <Function test_two[Firefox]>

由于oneTimeSetup1夹具的范围是类,我不确定为什么每个测试函数都在新的浏览器会话中运行。

我们可以有一个单一的Chrome浏览器会话,将执行我的test_one

import pytest
from pages.page1 import Page1


@pytest.mark.usefixtures("oneTimeSetup1")
class TestClassOne():

    @pytest.fixture(autouse=True)
    def classObject(self):
        self.page = Page1(self.driver)

    @pytest.mark.run(order=1)
    def test_one(self):
        self.page.methodA()
        print("This is Test One")

    @pytest.mark.run(order=2)
    def test_two(self):
        self.page.methodC()
        print("This is Test Two")

共有1个答案

狄阳华
2023-03-14

如评论中所述,问题来自通过pytest排序插件排序。这实际上是预期的行为(而不是bug,正如我最初写的那样),因为测试是显式排序的,并且由于夹具的使用,这种排序是在任何初始排序之后进行的。

如果使用pytest-order(pytest-order的一个分支,不再维护)和选项——放纵-排序,则可以更改此行为。这会更改排序的顺序,以便测试首先由插件排序,然后由夹具排序。请注意,在这种情况下,您必须将订单标记从run更改为order

...
    @pytest.mark.run(order=2)
    def test_one(self):
        self.page.methodA()
        print("This is Test One")

    @pytest.mark.order(1)
    def test_two(self):
        self.page.methodC()
        print("This is Test Two")

(我更改了顺序以显示排序有任何效果)。如果在没有选项的情况下运行this,您将获得(如原问题所示):

$ python -m pytest --collect-only
<Package tests>
  <Module test_one.py>
    <Class TestClassOne>
        <Function test_two[Chrome]>
        <Function test_two[Firefox]>
        <Function test_one[Chrome]>
        <Function test_one[Firefox]>

使用“放纵订购”选项,您可以得到:

$ python -m pytest --collect-only --indulgent-ordering
<Package tests>
  <Module test_one.py>
    <Class TestClassOne>
        <Function test_two[Chrome]>
        <Function test_one[Chrome]>
        <Function test_two[Firefox]>
        <Function test_one[Firefox]>

这使得该夹具可以作为类范围的夹具正常工作。

通常,您可以将该选项添加到您的pytest中。ini如果希望它对所有测试都有效:

[pytest]
addopts = --indulgent-ordering

免责声明:
我是pytest-order的作者。

 类似资料:
  • 我有一个类似这样的pytest测试: 现在,作为重构的一部分,我移动了这一行: 放入它自己的夹具中(在conftest.py文件中),因为它在其他地方使用。但是,除了直接导入fixture函数外,是否有其他方法在测试中引用它?我知道funcargs通常是调用fixture的方式,但是在本文中,当我想要调用fixture时,我不在测试函数中。

  • 谷歌在搜索答案方面让我失望,所以我现在转到这里。 我正在使用pytest和selenium设置一个自动web测试套件。在运行测试之前,我想先编写代码来检查chromewebdriver的更新。我认为最好的方法是使用fixture和webdriver\u manager模块。 我从以下开始,它起作用了 但每次测试都要检查webdriver的更新,这确实会减慢速度。因此,我尝试了这一点,但不知道如何让

  • 使用Robolectric的参数化测试 原文链接 : Parameterized testing with Robolectric 译文出自 : 开发技术前线 www.devtf.cn 译者 : Lollypo 校对者: Chaos 状态 : 校对完成 在目前的项目中我们使用Robolectric为Android应用程序编写单元测试,它一直都干的不错。最近我需要编写一个测试用例,通过每次使用不同的

  • 问题内容: 考虑以下Pytest: 该测试使用Pytest固定装置,其本身具有属性。在测试中迭代该属性,以便仅在每个in的断言均成立的情况下测试才通过。 但是,我实际上想做的是生成3个测试,其中2个应该通过,其中1个将失败。我试过了 但这导致 据我了解,在Pytest固定装置中,函数“成为”其返回值,但是在对参数进行参数化时,这似乎尚未发生。如何以所需的方式设置测试? 问题答案: 从 pytest

  • 主要内容:1. 使用XML传递参数,2. 通过@DataProvider传递参数,3. @DataProvider + 方法,4. @DataProvider + ITestContextTestNG中的另一个有趣的功能是参数化测试。 在大多数情况下,您会遇到业务逻辑需要大量测试的场景。 参数化测试允许开发人员使用不同的值一次又一次地运行相同的测试。 TestNG可以通过两种不同的方式将参数直接传递给测试方法: 使用 使用数据提供者 在本教程中,我们将向您展示如何通过XML 或将参数传递给方法。

  • 主要内容:1 参数化测试的介绍,2 使用@Parameter进行字段注入而不是构造函数,3 使用单个参数进行测试,4 识别单个测试用例1 参数化测试的介绍 自定义流道参数化实现参数化测试。运行参数化测试类时,将为测试方法和测试数据元素的叉积创建实例。 例如,要测试斐波那契函数,请编写: FibonacciTest的每个实例都将使用二元参数构造函数和方法中的数据值构造 @Parameters 。 2 使用@Parameter进行字段注入而不是构造函数 也可以将数据值直接注入字段中,而无需使用@Pa