Running Tests

Introduction

After creating test cases with PyTest, the next step is to execute them. PyTest provides a simple command-line interface that allows you to run individual test files, entire folders, specific test functions, or even a single test case within a file.

Whether you’re running one Selenium test during development or executing hundreds of automated tests in a regression suite, PyTest makes test execution fast, flexible, and easy to manage.

In this tutorial, you’ll learn how to run PyTest test cases, understand common execution commands, and execute a Selenium test using PyTest.


What is Running Tests?

Running Tests is the process of executing one or more PyTest test cases to verify that an application behaves as expected.

PyTest automatically discovers the selected test cases, executes them, and displays the test results in the terminal.

Example

Write Test Case

        │

        ▼

Run PyTest Command

        │

        ▼

PyTest Executes Test

        │

        ▼

Displays Pass / Fail Result

Why Run Tests Using PyTest?

Running tests with PyTest helps you:

  • Execute individual test cases.

  • Run complete test suites.

  • Verify application functionality.

  • Detect failures quickly.

  • Simplify regression testing.

  • Support Continuous Integration (CI/CD).


Common PyTest Commands

Run all discovered tests:

pytest

Run a specific test file:

pytest test_login.py

Run all tests inside a folder:

pytest tests/

Run a specific test function:

pytest test_login.py::test_valid_login

Display print() statements and browser logs during execution:

pytest -s

The -s option is especially useful when debugging Selenium tests because it allows console output and browser logs to be displayed.


Example

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


# Topic: 39. Test Execution - Running Tests
# Practice site: https://www.testmuai.com/selenium-playground/radiobutton-demo
# Run: pytest -s 39_examples/test_02_running_tests.py
#
# Run a file, folder, or node ID with pytest. The -s flag shows print output
# and keeps Selenium browser logs visible.


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

    try:
        driver.get("https://www.testmuai.com/selenium-playground/radiobutton-demo")
        male = driver.find_element(By.CSS_SELECTOR, "input[value='Male']")
        male.click()

        assert male.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_running_tests_radio_button():

Since the function name starts with test_, PyTest automatically discovers and executes it.


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/radiobutton-demo"
)

Navigates to the Radio Button Demo page.


Locate the Radio Button

male = driver.find_element(
    By.CSS_SELECTOR,
    "input[value='Male']"
)

Locates the Male radio button using a CSS selector.


Click the Radio Button

male.click()

Clicks the radio button.

After clicking, the radio button becomes selected.


Verify the Selection

assert male.is_selected()

The is_selected() method checks whether the radio button is selected.

If the radio button is selected, the test passes. Otherwise, the test fails.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Running This Test

Run the specific test file:

pytest -s 39_examples/test_02_running_tests.py

Run every test inside the 39_examples folder:

pytest -s 39_examples/

Run only this test function:

pytest -s 39_examples/test_02_running_tests.py::test_running_tests_radio_button

Practical Example

Suppose you’re working on a login feature.

Instead of executing the entire automation suite, you run only the login test while developing the feature.

Once the login functionality is complete, you execute the full regression suite.


Automation Testing Example

Consider an e-commerce automation framework.

The QA team can:

  • Execute only checkout tests before a payment release.

  • Run all regression tests before production deployment.

  • Execute a single failing test while debugging.

This saves time and improves testing efficiency.


Real-World Example

Running tests with PyTest is commonly used in:

  • Selenium automation frameworks

  • Regression testing

  • Smoke testing

  • Sanity testing

  • Continuous Integration (CI/CD)

  • Enterprise automation projects

Most CI/CD tools execute PyTest commands automatically whenever new code is committed.


Advantages of Running Tests with PyTest

  • Executes individual or multiple tests easily.

  • Supports file, folder, and function-level execution.

  • Displays detailed execution results.

  • Simplifies debugging.

  • Integrates with Selenium and CI/CD tools.

  • Scales well for large automation projects.


Common Mistakes Beginners Make

Running Python Files Instead of Using PyTest

Avoid executing Selenium test files directly using:

python test_login.py

Instead, execute them using:

pytest

This allows PyTest to manage test discovery, execution, and reporting.


Forgetting the -s Option During Debugging

Without the -s option, print() statements are captured by PyTest.

Use:

pytest -s

when you need to view console output during test execution.


Executing the Wrong Test File

Always verify that you’re running the intended test file or folder.


Running Multiple Test Scenarios While Debugging

When troubleshooting a failure, execute only the relevant test case instead of the entire suite.


Best Practices

  • Use pytest to execute test cases instead of running Python files directly.

  • Use the -s option while debugging Selenium tests.

  • Execute individual tests during development.

  • Run the complete test suite before deployment.

  • Organize tests into logical folders for easier execution.

  • Keep test cases independent of one another.


Conclusion

PyTest provides a flexible and efficient way to execute Selenium automation tests. Whether you’re running a single test function, an entire folder, or a complete automation suite, PyTest offers simple commands that make test execution straightforward. Understanding how to run tests effectively is an essential skill for developing, debugging, and maintaining professional Selenium automation frameworks.


Frequently Asked Questions (FAQs)

How do I run all PyTest test cases?

Use:

pytest

How do I run a specific test file?

Use:

pytest test_file.py

How do I run a single test function?

Use:

pytest test_file.py::test_function_name

What does the -s option do?

The -s option displays print() output and browser logs during test execution, making debugging easier.


Can I run all tests inside a folder?

Yes.

Use:

pytest folder_name/

to execute every discovered test inside the specified folder.


Key Takeaways

  • PyTest can execute individual test files, folders, or specific test functions.

  • Use pytest to run all discovered tests.

  • Use pytest -s to display console output and browser logs.

  • Test execution is simple, flexible, and scalable.

  • Running targeted tests speeds up development and debugging.

  • Efficient test execution is a fundamental part of professional Selenium automation with PyTest.