> For the complete documentation index, see [llms.txt](https://ultimatewebsolutions.gitbook.io/qa-knowledge-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ultimatewebsolutions.gitbook.io/qa-knowledge-base/test-automation/fixtures.md).

# Fixtures

#### What is a fixture in pytest?

A **fixture** is a piece of reusable setup code that prepares something your test needs **before the test runs**.

Think of it as:

> **Fixture = prepare something for the test.**

For example, instead of creating a browser/page in every test, you can create it once in a fixture and pass it to the test.

#### Simple example

```python
import pytest

@pytest.fixture
def username():
    return "admin"


def test_login(username):
    assert username == "admin"
```

Here:

```python
@pytest.fixture
def username():
    return "admin"
```

creates the fixture.

Then:

```python
def test_login(username):
```

asks pytest to provide that fixture.

#### Real QA example — browser

With Playwright:

```python
import pytest
from playwright.sync_api import Page

@pytest.fixture
def login_user(page: Page):
    page.goto("https://example.com")
    return page


def test_homepage(login_user):
    assert login_user.title() == "Example Domain"
```

The fixture prepares the browser by navigating to the website.

{% hint style="info" %}
A pytest fixture provides reusable test setup and optionally teardown. It helps avoid duplicating preparation code across tests.
{% endhint %}
