相关疑难解决方法(0)

在pytest中,conftest.py文件有什么用?

我最近发现了pytest.看起来很棒.但是,我觉得文档可能更好.

我正在尝试了解哪些conftest.py文件的用途.

在我(当前很小的)测试套件中,我conftest.py在项目根目录中有一个文件.我用它来定义我注入测试的灯具.

我有两个问题:

  1. 这是正确使用的conftest.py吗?它有其他用途吗?
  2. 我可以拥有多个conftest.py文件吗?我什么时候想这样做?将举例说明.

更一般地说,您如何conftest.py在py.test测试套件中定义文件的目的和正确使用?

python testing pytest

166
推荐指数
5
解决办法
4万
查看次数

pytest使用fixtures作为参数化中的参数

我想使用fixtures作为pytest.mark.parametrize的参数或具有相同结果的东西.

例如:

import pytest
import my_package

@pytest.fixture
def dir1_fixture():
    return '/dir1'

@pytest.fixture
def dir2_fixture():
    return '/dir2'

@pytest.parametrize('dirname, expected', [(dir1_fixture, 'expected1'), (dir2_fixture, 'expected2')]
def test_directory_command(dirname, expected):
    result = my_package.directory_command(dirname)
    assert result == expected
Run Code Online (Sandbox Code Playgroud)

夹具参数的问题在于夹具的每个参数都会在每次使用时运行,但我不希望这样.我希望能够根据测试选择使用哪种灯具.

python pytest

21
推荐指数
4
解决办法
5209
查看次数

按自定义顺序运行 Pytest 类

我正在 pycharm 中使用 pytest 编写测试。测试分为不同的类别。
我想指定某些类必须在其他类之前运行
我在 stackoverflow 上看到了各种问题(例如指定从文件运行 pytest 测试以及如何在所有其他测试之前运行方法)。
这些和其他各种问题想要选择特定的函数按顺序运行。fixtures据我了解,这可以使用或 with来完成pytest ordering
我不关心每个类的哪些函数首先运行。我所关心的是课程按照我指定的顺序运行。这可能吗?

python class function pytest pycharm

6
推荐指数
1
解决办法
3534
查看次数

标签 统计

pytest ×3

python ×3

class ×1

function ×1

pycharm ×1

testing ×1