0
0
PyTesttesting~5 mins

Test naming conventions in PyTest

Choose your learning style9 modes available
Introduction

Good test names help you and others understand what the test checks. Clear names make finding and fixing problems faster.

When writing new tests to keep them easy to read.
When reviewing tests to understand what each test does.
When debugging to quickly find which test failed.
When sharing tests with teammates for better collaboration.
When organizing tests in large projects to avoid confusion.
Syntax
PyTest
def test_function_name():
    assert something == expected

Test functions must start with test_ so pytest can find them.

Use descriptive names that explain what the test checks.

Examples
This test checks if adding 2 and 3 gives 5.
PyTest
def test_add_two_numbers():
    assert add(2, 3) == 5
This test checks if login works with correct username and password.
PyTest
def test_login_with_valid_credentials():
    assert login('user', 'pass') is True
This test checks that dividing by zero raises an error.
PyTest
import pytest

def test_divide_by_zero_raises_error():
    with pytest.raises(ZeroDivisionError):
        divide(5, 0)
Sample Program

This script has three tests for the add function. Each test name clearly says what it checks.

PyTest
import pytest

def add(a, b):
    return a + b

def test_add_positive_numbers():
    assert add(1, 2) == 3

def test_add_negative_numbers():
    assert add(-1, -1) == -2

def test_add_zero():
    assert add(0, 5) == 5
OutputSuccess
Important Notes

Always start test function names with test_ so pytest can detect them.

Use underscores to separate words for readability.

Keep names short but descriptive enough to understand the test purpose.

Summary

Test names must start with test_ for pytest to find them.

Good names describe what the test checks in simple words.

Clear test names help with debugging and teamwork.