pytest fixture has unexpected result

21 Views Asked by At

I'm learning pytest and as I was reading the part of the documentation that explains how fixtures can be requested more than once per test (return values are cached) I was wondering if the provided assertion in the example (see below) assert order == [first_entry] can also be written as assert append_first == [first_entry].

If I write the assertion like this it doesn't pass like the other one because append_first is None and I would like to know why it isn't ['a'] in this case since it should already have been executed.

# contents of test_append.py
import pytest


# Arrange
@pytest.fixture
def first_entry():
    return "a"


# Arrange
@pytest.fixture
def order():
    return []


# Act
@pytest.fixture
def append_first(order, first_entry):
    return order.append(first_entry)


def test_string_only(append_first, order, first_entry):
    # Assert 1: Pass
    assert order == [first_entry]
    
    # Assert 2: AssertionError: assert None == ['a']
    assert append_first == [first_entry]
0

There are 0 best solutions below