Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest:从父类继承fixture_Python_Flask_Pytest_Connexion - Fatal编程技术网

Python Pytest:从父类继承fixture

Python Pytest:从父类继承fixture,python,flask,pytest,connexion,Python,Flask,Pytest,Connexion,我有几个测试用例来测试基于flask/connexion的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 a

我有几个测试用例来测试基于flask/connexion的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'

什么是将fixture继承到子类的正确方法?

所以在谷歌搜索关于fixture的更多信息后,我遇到了

因此,需要采取两个步骤

  • 删除unittest测试用例继承
  • @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