0
0
PyTesttesting~5 mins

Addopts for default options in PyTest

Choose your learning style9 modes available
Introduction

Addopts lets you set default command-line options for pytest. This saves time by not typing options every time you run tests.

You want pytest to always show detailed test results without typing -v each time.
You want to run tests with a specific marker by default.
You want to set a default timeout for all tests.
You want to always generate a test report file automatically.
You want to ignore certain warnings every time you run tests.
Syntax
PyTest
[pytest]
addopts = <options>

Put this in a pytest.ini file in your project root.

Options are the same as you type on the command line.

Examples
This makes pytest always run in verbose mode, showing each test name.
PyTest
[pytest]
addopts = -v
This runs only tests marked with @pytest.mark.smoke by default.
PyTest
[pytest]
addopts = -m smoke
This stops after 2 failures and hides warnings automatically.
PyTest
[pytest]
addopts = --maxfail=2 --disable-warnings
Sample Program

This config runs pytest in verbose mode and stops after 1 failure. The sample tests include one passing, one failing, and one skipped test.

PyTest
[pytest]
addopts = -v --maxfail=1

# test_sample.py
import pytest

def test_pass():
    assert 1 == 1

def test_fail():
    assert 1 == 2

def test_skip():
    pytest.skip('skip this test')
OutputSuccess
Important Notes

You can combine multiple options in addopts separated by spaces.

If you want to override addopts options, you can still pass options directly on the command line.

Use pytest --help to see all available options you can add.

Summary

Addopts sets default pytest command-line options in pytest.ini.

This saves time and ensures consistent test runs.

Options are the same as typing them manually when running pytest.