python - Pytest:从父类继承夹具
问题描述
我有几个测试用例来测试基于烧瓶/连接的 api 的端点。
现在我想将它们重新排序为类,所以有一个基类:
import pytest
from unittest import TestCase
# Get the connexion app with the database configuration
from app import app
class ConnexionTest(TestCase):
"""The base test providing auth and flask clients to other tests
"""
@pytest.fixture(scope='session')
def client(self):
with app.app.test_client() as c:
yield c
现在我有另一堂课,里面有我的实际测试用例:
import pytest
from ConnexionTest import ConnexionTest
class CreationTest(ConnexionTest):
"""Tests basic user creation
"""
@pytest.mark.dependency()
def test_createUser(self, client):
self.generateKeys('admin')
response = client.post('/api/v1/user/register', json={'userKey': self.cache['admin']['pubkey']})
assert response.status_code == 200
现在不幸的是我总是得到一个
TypeError: test_createUser() missing 1 required positional argument: 'client'
将夹具继承到子类的正确方法是什么?
解决方案
因此,在谷歌搜索有关固定装置的更多信息后,我发现了这篇文章
所以有两个必需的步骤
- 删除 unittest TestCase 继承
- 将
@pytest.mark.usefixtures()
装饰器添加到子类以实际使用夹具
在代码中它变成
import pytest
from app import app
class TestConnexion:
"""The base test providing auth and flask clients to other tests
"""
@pytest.fixture(scope='session')
def client(self):
with app.app.test_client() as c:
yield c
现在是孩子班
import pytest
from .TestConnexion import TestConnexion
@pytest.mark.usefixtures('client')
class TestCreation(TestConnexion):
"""Tests basic user creation
"""
@pytest.mark.dependency(name='createUser')
def test_createUser(self, client):
self.generateKeys('admin')
response = client.post('/api/v1/user/register', json={'userKey': self.cache['admin']['pubkey']})
assert response.status_code == 200
推荐阅读
- intellij-idea - IntelliJ IDEA 控制台似乎在行之间有额外的“填充”,导致背景颜色高于实际行高
- javascript - 无法读取未定义的属性“库存”
- typescript - Vue + Typescript + 汇总
- python - 如何在 Python 中结合 groupby、rolling 和多列的创建
- php - 无法猜测 MIME 类型,因为没有可用的猜测器(您是否启用了 php_fileinfo 扩展?)。莱维尔 8
- excel - VBA:非连续范围 - 对象不支持此属性或方法
- scala - 错误:“数据源 org.apache.spark.sql.cassandra 不支持流式读取”
- java - 在 Firebase 中更新 displayName 会出现语法错误
- c - 即使我想要它,程序也没有接受输入
- angular - 如何从凌晨 4 点到凌晨 3 点 59 分实施工作日?