jeudi 17 octobre 2019

What's the correct way to return structured data from a fixture?

Currently I have a setup like this:

./
  src/
    mylib.py
  tests/
    __init__.py
    conftest.py
    test_mylib.py

mylib.py contains:

def add(x, y):
    return x + y

conftest.py contains:

import attr
import pytest

@attr.s(slots=True)
class AdditionTestCase:
    input1 = attr.ib()
    input2 = attr.ib()
    output_expected = attr.ib()

@pytest.fixture
def addition_test_case() -> AdditionTestCase:
    # make up some data
    return AdditionTestCase(1, 2, 3)

and test_mylib.py contains:

from mylib import add
from tests.conftest import AdditionTestCase

def test_add(addition_test_case: AdditionTestCase):
    input1 = addition_test_case.input1
    input2 = addition_test_case.input2
    output_expected = addition_test_case.output_expected

    output_actual = add(input1, input2)
    assert output_actual == output_expected

Is this problematic for any reason? Is there a better way to do it? I could move the type definition outside conftest.py if importing from it is considered bad practice.

Aucun commentaire:

Enregistrer un commentaire