Test Discovery

Introduction

As the number of automated test cases increases, manually specifying which tests to run becomes impractical. PyTest solves this problem through Test Discovery, a feature that automatically locates and collects test files, test classes, and test functions based on predefined naming conventions.

With Test Discovery, you simply execute the pytest command, and PyTest automatically finds all eligible test cases without requiring any additional configuration. This makes test execution faster, more organized, and easier to maintain.

In this tutorial, you’ll learn how PyTest discovers test cases, understand the naming conventions it follows, and see how Selenium test scripts are automatically collected and executed.


What is Test Discovery?

Test Discovery is the process by which PyTest automatically searches for and identifies test cases that are ready to be executed.

Instead of manually listing every test, PyTest scans your project and collects files, classes, and functions that follow its naming conventions.

Example

Project Folder

│
├── test_login.py
├── test_search.py
├── login.py
└── helper.py

        │

        ▼

PyTest Discovery

        │

        ▼

Collected Tests

✓ test_login.py
✓ test_search.py

Only files and functions that follow PyTest’s naming rules are collected.


Why Use Test Discovery?

Test Discovery helps you:

  • Automatically locate test cases.

  • Eliminate manual test selection.

  • Execute large test suites efficiently.

  • Improve project organization.

  • Reduce configuration effort.

  • Simplify automation maintenance.


How Test Discovery Works

By default, PyTest automatically discovers:

Test Files

Files whose names begin with:

test_*.py

or end with:

*_test.py

Test Functions

Functions whose names begin with:

def test_login():

Test Classes

Classes whose names begin with:

class TestLogin:

Only methods inside these classes whose names begin with test_ are executed as test cases.


Example

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 39. Test Execution - Test Discovery
# Practice site: https://www.testmuai.com/selenium-playground/checkbox-demo
# Run: pytest -s 39_examples/test_01_test_discovery.py
#
# PyTest discovers functions whose names start with test_ (or classes that
# start with Test). Place files as test_*.py for automatic collection.


def test_test_discovery_collects_this_function():
    driver = webdriver.Chrome()

    try:
        driver.get("https://www.testmuai.com/selenium-playground/checkbox-demo")
        checkbox = driver.find_element(By.ID, "isAgeSelected")
        checkbox.click()

        assert checkbox.is_selected()
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

from selenium import webdriver
from selenium.webdriver.common.by import By

These modules are used to launch the Chrome browser and locate web elements.


Create a Test Function

def test_test_discovery_collects_this_function():

The function name starts with test_.

PyTest automatically recognizes this function as a test case during the discovery process.

No additional registration is required.


Create a Chrome Browser Instance

driver = webdriver.Chrome()

Launches a new Chrome browser session.


Open the Practice Website

driver.get(
    "https://www.testmuai.com/selenium-playground/checkbox-demo"
)

Navigates to the Checkbox Demo page.


Locate the Checkbox

checkbox = driver.find_element(
    By.ID,
    "isAgeSelected"
)

Locates the checkbox using its ID.


Click the Checkbox

checkbox.click()

Clicks the checkbox.

The checkbox changes from an unchecked state to a checked state.


Verify the Checkbox Selection

assert checkbox.is_selected()

The is_selected() method returns True if the checkbox is selected.

The assert statement verifies that the checkbox was successfully checked.

If the checkbox is not selected, the test fails.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Running the Test

To execute this test, run:

pytest -s 39_examples/test_01_test_discovery.py

To execute all discovered tests in the project, simply run:

pytest

PyTest automatically discovers every eligible test file and executes the collected test cases.


Practical Example

Suppose your automation project contains hundreds of login, registration, and checkout tests.

Instead of executing each file individually, you simply run:

pytest

PyTest automatically discovers every valid test file and executes all test cases.


Automation Testing Example

Consider an e-commerce automation framework containing:

  • Login tests

  • Search tests

  • Cart tests

  • Payment tests

  • Order tests

PyTest scans the project, discovers every test file, and executes all collected tests automatically.


Real-World Example

Test Discovery is widely used in:

  • Selenium automation frameworks

  • Regression testing

  • Continuous Integration (CI/CD)

  • Enterprise QA projects

  • API automation

  • Data-driven testing

Most automation frameworks rely on Test Discovery to execute large numbers of tests without manual configuration.


Advantages of Test Discovery

  • Automatically finds test cases.

  • Eliminates manual test registration.

  • Simplifies test execution.

  • Supports large automation projects.

  • Encourages consistent naming conventions.

  • Integrates seamlessly with CI/CD pipelines.


Common Mistakes Beginners Make

Using Incorrect File Names

Files that do not follow PyTest’s naming convention may not be discovered.

Use names such as:

test_login.py

or

login_test.py

Naming Test Functions Incorrectly

Functions that do not begin with test_ are ignored during discovery.

Always use names such as:

def test_login():

Assuming Every Python File is Executed

PyTest only discovers files and functions that follow its naming rules.

Regular Python scripts are ignored.


Mixing Multiple Test Scenarios

Keep one test scenario per test function.

This improves readability and simplifies debugging.


Best Practices

  • Name test files using test_*.py or *_test.py.

  • Name every test function beginning with test_.

  • Keep test cases independent.

  • Organize tests into topic-based folders.

  • Execute tests using the pytest command.

  • Follow consistent naming conventions throughout the project.


Conclusion

Test Discovery is one of PyTest’s most powerful features. By automatically locating test files, classes, and functions based on simple naming conventions, PyTest eliminates manual configuration and simplifies test execution. Following the recommended naming conventions ensures that your Selenium test cases are discovered automatically, making your automation projects cleaner, more scalable, and easier to maintain.


Frequently Asked Questions (FAQs)

What is Test Discovery?

Test Discovery is the automatic process by which PyTest locates and collects test cases for execution.


Which files does PyTest discover?

By default, PyTest discovers files named:

test_*.py

or

*_test.py

Which functions are executed as test cases?

Functions whose names begin with:

def test_...

are automatically collected and executed.


Does PyTest discover test classes?

Yes.

Classes whose names begin with Test are discovered, and their methods beginning with test_ are executed.


Which command executes all discovered tests?

Run:

pytest

PyTest automatically discovers and executes every eligible test in the project.


Key Takeaways

  • Test Discovery automatically locates test cases for execution.

  • PyTest discovers files named test_*.py or *_test.py.

  • Test functions should begin with test_.

  • Test classes should begin with Test.

  • Following PyTest naming conventions ensures automatic test collection.

  • Test Discovery simplifies execution and improves the scalability of Selenium automation projects.