0
0
PyTesttesting~10 mins

Test file and function naming conventions in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that pytest recognizes test files and test functions based on proper naming conventions. It verifies that a test function named with the prefix test_ inside a file named with the prefix test_ is discovered and executed by pytest.

Test Code - PyTest
PyTest
import pytest

def test_addition():
    assert 2 + 3 == 5
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Pytest starts and scans the current directory for test files matching pattern 'test_*.py' or '*_test.py'.Test file named 'test_example.py' is present with the test function inside.-PASS
2Pytest loads 'test_example.py' and discovers the function 'test_addition' because it starts with 'test_'.Test function 'test_addition' is ready to run.-PASS
3Pytest executes the function 'test_addition'.Inside the test function, the expression 2 + 3 == 5 is evaluated.Assert that 2 + 3 equals 5.PASS
4Pytest reports the test result as passed.Test run completes successfully with no failures.-PASS
Failure Scenario
Failing Condition: Test function or file does not follow pytest naming conventions (e.g., function not starting with 'test_').
Execution Trace Quiz - 3 Questions
Test your understanding
Why does pytest not run a function named 'addition_test' inside 'test_example.py'?
ABecause the file name does not start with 'test_'.
BBecause the function name does not start with 'test_'.
CBecause pytest only runs functions inside classes.
DBecause the function is missing an assert statement.
Key Result
Following pytest's naming conventions for test files and functions is essential for automatic test discovery and execution.