Created
November 21, 2021 16:16
-
-
Save hackebrot/cf48826d3f89c9cffc48b20cafdc4af8 to your computer and use it in GitHub Desktop.
Tests using pytest fixtures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass, field | |
from typing import List | |
import pytest | |
@dataclass(frozen=True) | |
class Ingredient: | |
name: str | |
plant_based: bool | |
@dataclass(frozen=True) | |
class Sushi: | |
name: str | |
ingredients: List[Ingredient] = field(default_factory=list) | |
def __contains__(self, ingredient: Ingredient): | |
return ingredient in self.ingredients | |
@pytest.fixture | |
def salmon(): | |
return Ingredient(name="Salmon", plant_based=False) | |
@pytest.fixture | |
def rice(): | |
return Ingredient(name="Rice", plant_based=True) | |
@pytest.fixture | |
def nori(): | |
return Ingredient(name="Nori", plant_based=True) | |
@pytest.fixture | |
def sake_nigiri(salmon, rice, nori): | |
return Sushi(name="Sake Nigiri", ingredients=[salmon, rice, nori]) | |
def test_sake_nigiri_contains_salmon(sake_nigiri, salmon): | |
assert salmon in sake_nigiri | |
def test_sake_nigiri_contains_rice(sake_nigiri): | |
assert rice in sake_nigiri | |
def test_sake_nigiri_contains_nori(sake_nigiri, nori): | |
assert nori in sake_nigiri |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment