python – 从夹具内部跳过测试

假设我有一个需要实时数据库的夹具.

如果实时数据库不存在,我想跳过依赖于该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

@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
点赞