Pytest: наследовать приспособление от родительского класса - PullRequest
0 голосов
/ 21 февраля 2019

У меня есть пара тестов для проверки конечных точек 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 Ответ

0 голосов
/ 21 февраля 2019

Так что после поиска в Google для получения дополнительной информации о приборах я наткнулся на этот пост

Итак, было два необходимых шага

  1. Удалите наследуемое наследование 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
...