Command Line Options

Introduction

When working with PyTest, you don’t always need to modify your test code to change how tests are executed. Instead, you can use Command Line Options to control test execution directly from the terminal.

PyTest provides many built-in command-line options that allow you to display detailed test output, run only selected tests, filter tests using markers or keywords, stop execution after failures, generate reports, and much more.

These options make test execution faster, more flexible, and easier to manage, especially in large Selenium automation projects.

In this tutorial, you’ll learn the most commonly used PyTest command-line options, how they work, and how to access command-line configuration from within a test.


What are Command Line Options?

Command Line Options are arguments passed to the pytest command when executing tests.

These options modify how PyTest collects, executes, and displays test results.

For example:

pytest -v tests/

Here:

  • pytest starts the test execution.

  • -v enables verbose output.


Why Use Command Line Options?

Command-line options help you:

  • Run selected tests.

  • Display detailed test results.

  • Filter tests using markers or keywords.

  • Stop execution after failures.

  • Generate HTML reports.

  • Control console output.

  • Improve debugging.


Commonly Used Command Line Options

Option Description
-s Displays print() statements by disabling output capture.
-v Runs tests in verbose mode with detailed output.
-q Runs tests in quiet mode with minimal output.
-k Runs tests whose names match a specified keyword.
-m Runs tests with a specific marker such as smoke or regression.
--maxfail=1 Stops test execution after the first failure.
--html=report.html Generates an HTML test execution report (requires the pytest-html plugin).

Example

import pytest


# Topic: 41. Advanced PyTest - Command Line Options
# Practice site: n/a (project-level example)
# Run: pytest -s -q 41_examples/test_06_command_line_options.py
#
# Common options: -s (no capture), -v (verbose), -q (quiet), -k (name filter),
# -m (mark filter), --maxfail=1, --html=report.html.


def test_command_line_options(pytestconfig):
    assert pytestconfig.getoption("capture") in {"no", "sys", "fd", "tee-sys"}

Understanding the Code

Import Required Library

import pytest

This imports the PyTest framework.

Although this example does not directly call PyTest functions, it represents a PyTest test file and follows the standard project structure.


Access the PyTest Configuration

def test_command_line_options(pytestconfig):

The pytestconfig fixture is a built-in PyTest fixture that provides access to the current PyTest configuration.

It allows you to read command-line options and configuration values during test execution.


Read a Command-Line Option

pytestconfig.getoption("capture")

The getoption() method retrieves the value of a command-line option used when running the test.

In this example, it retrieves the current capture mode, which controls how PyTest handles console output.


Verify the Capture Mode

assert pytestconfig.getoption("capture") in {
    "no",
    "sys",
    "fd",
    "tee-sys"
}

This assertion verifies that the current capture mode is one of PyTest’s supported values.

The possible values are:

Value Meaning
no Output capture is disabled (-s).
sys Captures Python’s standard output and error streams.
fd Captures operating system file descriptors (default).
tee-sys Captures output while also displaying it in the console.

If the returned value is not one of these valid modes, the test fails.


Practical Example

Suppose you’re debugging a Selenium login test and want to see all print() statements during execution.

Instead of changing your code, simply run:

pytest -s

PyTest will display all console output while the test runs.


Automation Testing Example

Consider a Selenium automation project containing hundreds of test cases.

During daily smoke testing, the QA engineer runs:

pytest -m smoke

To execute only smoke tests.

If a failure occurs and the execution should stop immediately:

pytest --maxfail=1

For debugging, detailed test information can be displayed using:

pytest -v

Real-World Example

Command-line options are commonly used in:

  • Selenium automation frameworks

  • CI/CD pipelines

  • Jenkins jobs

  • GitHub Actions

  • Azure DevOps pipelines

  • Regression testing

  • Smoke testing

  • Enterprise automation projects

Automation engineers use these options daily to control test execution without modifying test code.


Advantages of Command Line Options

  • No code changes required.

  • Easy test filtering.

  • Faster debugging.

  • Flexible test execution.

  • Better console output control.

  • Improved automation workflow.


Common Mistakes Beginners Make

Forgetting Option Syntax

Every command-line option must be written correctly.

For example:

pytest -v

not

pytest v

Confusing -v and -q

  • -v displays detailed information.

  • -q minimizes console output.

Use the option that best suits your testing needs.


Using Undefined Markers

Before running:

pytest -m smoke

ensure that the smoke marker has been registered in pytest.ini.


Assuming Every Option Is Built Into PyTest

Some options, such as:

--html=report.html

require additional plugins like pytest-html.


Best Practices

  • Use -v while developing and debugging tests.

  • Use -q when only a concise summary is needed.

  • Use -m to organize Smoke, Regression, and Sanity test execution.

  • Use -k to quickly execute tests by name.

  • Use --maxfail to stop execution after critical failures.

  • Store commonly used options inside pytest.ini using addopts when appropriate.


Conclusion

PyTest Command Line Options provide a powerful way to control how tests are executed without modifying the test code. Whether you need detailed output, selective execution, marker filtering, or report generation, these options make Selenium automation projects more efficient, flexible, and easier to manage. Learning the most commonly used command-line options is an essential skill for every automation tester.


Frequently Asked Questions (FAQs)

What are PyTest Command Line Options?

They are arguments passed to the pytest command to control test execution and output.


What does the -v option do?

It runs tests in verbose mode, displaying detailed information about each test.


What does the -s option do?

It disables output capture, allowing print() statements to appear in the console during test execution.


What is the purpose of the -m option?

It runs only the tests associated with a specified marker, such as smoke or regression.


What is pytestconfig?

It is a built-in PyTest fixture that provides access to command-line options and project configuration during test execution.


Key Takeaways

  • Command Line Options control how PyTest executes tests.

  • The pytestconfig fixture provides access to runtime configuration.

  • getoption() retrieves the value of a command-line option.

  • Common options include -s, -v, -q, -k, -m, and --maxfail.

  • Some options, such as --html, require additional PyTest plugins.

  • Mastering command-line options makes Selenium + PyTest test execution faster, more flexible, and easier to manage.