I am running pytest via python script (not command line). I am trying to write pytest session outputs (test summary, tests collected etc.) to a seperate log file. I came across answers for writing the seperate log file when running pytest from command line How to save pytest's results/logs to a file? Here my console output and log file out put are not same.
#test_functions.py
import pytest
import logging
LOGGER = logging.getLogger(__name__)
@pytest.fixture
def input_value():
input = 39
return input
def test_divisible_by_3(input_value):
LOGGER.info('Running the test 1')
assert input_value % 3 == 0
def test_divisible_by_6(input_value):
LOGGER.info('Running the test 2')
assert input_value % 6 == 0
#script.py
import pytest
import logging
log = logging.getLogger(__name__)
pytest.main(args= ["test_functions.py","--log-level=DEBUG","--log-file=py_test_report.log"])
Execution
python script.py
I can see that py_test_report.log gets created, but not pytest summary as that displayed in console
console out put
platform linux -- Python 3.7.12, pytest-7.4.0, pluggy-1.2.0
rootdir: /home/ec2-user/SageMaker/pytest_examples
configfile: pytest.ini
plugins: anyio-3.7.1
collected 2 items
test_functions.py::test_divisible_by_3
--------------------------------------------------------------------------------------------- live log call ----------------------------------------------------------------------------------------------
INFO test_functions:test_functions.py:13 Running the test 1
PASSED [ 50%]
test_functions.py::test_divisible_by_6
--------------------------------------------------------------------------------------------- live log call ----------------------------------------------------------------------------------------------
INFO test_functions:test_functions.py:17 Running the test 2
FAILED [100%]
================================================================================================ FAILURES ================================================================================================
__________________________________________________________________________________________ test_divisible_by_6 ___________________________________________________________________________________________
input_value = 39
def test_divisible_by_6(input_value):
LOGGER.info('Running the test 2')
> assert input_value % 6 == 0
E assert (39 % 6) == 0
test_functions.py:18: AssertionError
------------------------------------------------------------------------------------------- Captured log call --------------------------------------------------------------------------------------------
INFO test_functions:test_functions.py:17 Running the test 2
======================================================================================== short test summary info =========================================================================================
FAILED test_functions.py::test_divisible_by_6 - assert (39 % 6) == 0
====================================================================================== 1 failed, 1 passed in 0.07s =======================================================================================
output of log file
INFO test_functions:test_functions.py:13 Running the test 1
INFO test_functions:test_functions.py:17 Running the test 2
I would like to have same out put in log file as displayed in console.