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'

将固定装置继承到子类的正确方法是什么?

因此,在搜索更多有关固定装置的信息之后,我遇到了这篇文章

所以有两个必需的步骤

  1. 删除Unitest TestCase继承
  2. @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

相关内容

  • 没有找到相关文章

最新更新