python - 从 fixture 内部跳过测试

标签 python testing pytest

假设我有一个需要实时数据库的装置。

如果实时数据库不存在,我想跳过依赖于该 fixture 的测试。

目前,我必须手动标记要跳过的测试,这感觉很多余:

@pytest.fixture
def db_client():
  DB_URI = os.getenv('DB_URI')
  # Set up DB client and yield it

@pytest.mark.skipif(not os.getenv('DB_URI'))
def test_some_feature(db):
  # Use db fixture
  ...

最佳答案

调用pytest.skip fixture 内部:

@pytest.fixture
def db():
    db_uri = os.getenv('DB_URI', None)
    if not db_uri:
        pytest.skip('No database available')
    else:
        # Set up DB client and yield it

关于python - 从 fixture 内部跳过测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52330369/

相关文章:

Python:将整数写入二进制文件

testing - Ember.js 中的验收测试更改地址栏中的 URL

python - 如何测试新的 Django 1.10+ 中间件类

python - Pytest 版本冲突错误

python - Pandas - 如果少于 N,则删除唯一行

python - 设置超过2个值取决于条件,python

python - 高斯数据拟合因 x 数据的位置而异

java - JUnit 测试说明

python - 名为断言的函数在模拟导入类上失败

python - 在 py.test 中是否可以报告测试运行中生成的任意值?