This is an old revision of the document!
hierarchy typically looks like:
TODO
assert
statement.
assert interface.status == "up"
(@ is a decorator. special function that modifies the behavior of another function or class without changing its actual code)
@pytest.mark.sanity
(categorization)
@pytest.mark.skip(reason="Not implemented")
(skipping)
@aetest.loop
,
@aetest.setup
,
@aetest.cleanup
Example1:: The greeting fixture is provided automatically to both test functions.
import pytest
@pytest.fixture
def greeting():
return "hello world"
def test_uppercase(greeting):
assert greeting.upper() == "HELLO WORLD"
def test_length(greeting):
assert len(greeting) == 11
Fixture with Setup and Teardown: The yield keyword pauses the fixture until the test finishes, then resumes for any cleanup.
import pytest
@pytest.fixture
def temp_file(tmp_path):
file = tmp_path / "sample.txt"
file.write_text("Hello")
yield file
# Cleanup code here (e.g. delete or log)
Dependent Fixtures : Fixtures can call other fixtures automatically.
@pytest.fixture
def user_data():
return {"name": "Alice", "id": 1}
@pytest.fixture
def user_token(user_data):
return f"TOKEN-{user_data['id']}"
def test_token(user_token):
assert user_token.startswith("TOKEN-")
Parametrised Fixtures : The test runs once for each value of the user parameter.
@pytest.fixture(params=["alice", "bob"])
def user(request):
return request.param
def test_starts_lowercase(user):
assert user[0].islower()
[[https://docs.pytest.org/en/stable/how-to/fixtures.html|Pytest Fixture Documentation]]
[[https://realpython.com/pytest-python-testing/#using-pytest-fixtures|Real Python: Pytest Fixtures]]
[[https://docs.pytest.org/en/stable/how-to/fixtures.html#parametrizing-fixtures|Parametrising Fixtures]]
[[https://gist.github.com/kwaldrip/0ed22c6e3c8b476b8a84cf3c137b3e15|Pytest Fixture Cheat Sheet (Gist)]]
@pytest.mark.parametrize
.
@pytest.mark.parametrize("ip", ["8.8.8.8", "1.1.1.1"]) def test_ping(ip): assert ping(ip)
aetest.Testcase
, containing test sections.
setup
,
test
, and
cleanup
.
log
object).