15

When a test fails, there's an output indicating the context of the test, e.g.

=================================== FAILURES ===================================
______________________________ Test.test_sum_even ______________________________

numbers = [2, 4, 6]

    @staticmethod
    def test_sum_even(numbers):
        assert sum(numbers) % 2 == 0
>       assert False
E       assert False

test_preprocessing.py:52: AssertionError

What if I want the same thing for passed tests as well? so that I can have a quick check on the parameters that get passed to the tests are correct?

I tried command line options line --full-trace, -l, --tb long, and -rpP, but none of them works.

Any idea?

Uduse
  • 1,491
  • 1
  • 13
  • 18

4 Answers4

15

Executing pytest with the --verbose flag will cause it to list the fully qualified name of every test as it executes, e.g.,:

tests/dsl/test_ancestor.py::TestAncestor::test_finds_ancestor_nodes
tests/dsl/test_and.py::TestAnd::test_aliased_as_ampersand
tests/dsl/test_and.py::TestAnd::test_finds_all_nodes_in_both_expressions
tests/dsl/test_anywhere.py::TestAnywhere::test_finds_all_nodes_when_no_arguments_given_regardless_of_the_context
tests/dsl/test_anywhere.py::TestAnywhere::test_finds_multiple_kinds_of_nodes_regardless_of_the_context
tests/dsl/test_anywhere.py::TestAnywhere::test_finds_nodes_regardless_of_the_context
tests/dsl/test_axis.py::TestAxis::test_finds_nodes_given_the_xpath_axis
tests/dsl/test_axis.py::TestAxis::test_finds_nodes_given_the_xpath_axis_without_a_specific_tag
Ian Lesperance
  • 4,961
  • 1
  • 26
  • 28
5

If you are just asking for standard output from passed test cases, then you need to pass the -s option to pytest to prevent capturing of standard output. More info about standard output suppression is available in the pytest docs.

bbqwings
  • 131
  • 3
  • 1
    Not the standard output from the passed test cases, but the output from pytest as if the tests fail. – Uduse Feb 08 '18 at 08:21
2

pytest doesn't have this functionality. What it does is showing you the error from the exception when an assertion fails.

A workaround is to explicitly include the information you want to see from the passing tests by using python's logging module and then use the caplog fixture from pytest.

For example one version of a func.py could be:

import logging

logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger('my-even-logger')


def is_even(numbers):
    res = sum(numbers) % 2 == 0
    if res is True:
        log.warning('Sum is Even')
    else:
        log.warning('Sum is Odd')

#... do stuff ...

and then a test_func.py:

import logging
import pytest
from func import is_even

@pytest.fixture
def my_list():
    numbers = [2, 4, 6]
    return numbers
 
def test_is_even(caplog, my_list):
    with caplog.at_level(logging.DEBUG, logger='my-even-logger'):
        is_even(my_list)
    assert 'Even' in caplog.text

If you run pytest -s test_even.py and since the test passes, the logger shows you the following message:

test_even.py WARNING:my-sum-logger:Sum is Even

mobiuscreek
  • 421
  • 6
  • 6
  • I had trouble seeing logs for INFO level. Adding `log_cli=true` in `pytest.ini` solved it. Found the solution [here](https://stackoverflow.com/a/53886088/7283201). – Sadman Sakib Feb 05 '22 at 04:31
-1

simple solution

I just raised an exception at end of the test

def test_server_run():
    print("ok")
    assert True
    raise Exception()
Shimon Doodkin
  • 4,310
  • 34
  • 37