Pytest:从父级继承夹具 class
Pytest: Inherit fixture from parent class
我有几个测试用例来测试基于 flask/connexion 的 api 的端点。
现在我想将它们重新排序为 classes,所以有一个基数 class:
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
现在我有了另一个 class 我的实际测试用例:
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'
将 fixture 继承给 subclasses 的正确方法是什么?
因此,在谷歌搜索有关固定装置的更多信息后,我遇到了
因此需要执行两个步骤
- 移除 unittest TestCase 继承
- 将
@pytest.mark.usefixtures()
装饰器添加到 child class 以实际使用 fixture
在代码中变成
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
现在 child class
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
我有几个测试用例来测试基于 flask/connexion 的 api 的端点。
现在我想将它们重新排序为 classes,所以有一个基数 class:
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
现在我有了另一个 class 我的实际测试用例:
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'
将 fixture 继承给 subclasses 的正确方法是什么?
因此,在谷歌搜索有关固定装置的更多信息后,我遇到了
因此需要执行两个步骤
- 移除 unittest TestCase 继承
- 将
@pytest.mark.usefixtures()
装饰器添加到 child class 以实际使用 fixture
在代码中变成
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
现在 child class
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