0

Basically I am creating a custom log file where after every test executes I need to write to log file whether it passed or failed. I am aware of answers related to hooks, but I assume there should be some simpler way to capture this data.

Many Thanks in advance!

Working with pytest testing framework in Python. I have created a custom log file and want to add PASS FAIL results in that log file as and when test case execution finishes for each test.

Abhilash
  • 19
  • 1
  • How are you running pytest? Please also show example pytest output vs what each line is supposed to generate in your log. – h4z3 Jul 25 '23 at 11:57
  • Could be a duplicate of this one as well: https://stackoverflow.com/questions/31793540/how-to-save-pytests-results-logs-to-a-file – Philippe Oger Jul 25 '23 at 11:59
  • @h4z3 after each test executes in pytest we get to see the test result wheteher it is passed or failed on the screen, so I wanted to know if there is a way to capture it during test excecution. Iam running pytest from cmd. – Abhilash Jul 25 '23 at 12:04
  • Do you really want one log-file for each test-_case_ or do I misunderstand you? – julaine Jul 25 '23 at 12:10
  • @julaine no will create a log file at the start and then will update the logs for each test in that same log file. I am able to write logs but I want a way to capture whether a test passed or failed, so that I can populate the log file with that information as well. – Abhilash Jul 25 '23 at 12:14

3 Answers3

0

The following should do the trick

python your_test.py > your_test_output.log
Philippe Oger
  • 1,021
  • 9
  • 21
  • the logfile is created during script execution, and I want to populate that new log file with the test result – Abhilash Jul 25 '23 at 12:06
0

To write test results for each test-case which is executed (or skipped), use the -v (verbose) switch for pytest. It is one of many options to control the output of pytest, see here: https://docs.pytest.org/en/7.1.x/how-to/output.html

pytest -v will produce output like this:

=========================== test session starts ============================
collecting ... collected 4 items

test_verbosity_example.py::test_ok PASSED                            [ 25%]
test_verbosity_example.py::test_words_fail FAILED                    [ 50%]

This will be colored if pytest detects stdout to be a terminal but work fine when piped to text files also.

julaine
  • 382
  • 3
  • 12
  • Thanks for the answer, but as I mentioned I am not aware of the log file name since it is created during script execution, so I cannot provide a name to pipe it to..... I am creating the log file using python logger. – Abhilash Jul 25 '23 at 12:50
  • So you want pytest to use your logger for outputting? I am not sure that is possible, and I fail what problem you are trying to solve. If your test run creates a log-file, why do you care if it gets created by the shell or by the python-logging-library? – julaine Jul 25 '23 at 12:55
  • Thanks, I taught of creating a separate log file, because I wanted to add some information related to each tests as the test progresses and I am able to use python logger to achieve this and I am able to write to log file. Just I am not finding a way to determine programmatically if a test passed or failed during the execution. – Abhilash Jul 25 '23 at 13:10
  • @Abhilash Maybe this helps: you can prevent pytest from capturing your tests stdout-output. Then you can have your own outputs and pytests output in the same file (if that is what you are trying to achieve) – julaine Jul 25 '23 at 13:12
  • 1
    @Abhilash maybe the top two answers of this question can give you ideas: https://stackoverflow.com/questions/14405063/how-can-i-see-normal-print-output-created-during-pytest-run?rq=1 – julaine Jul 25 '23 at 13:13
0

If you don't mind using a plugin. The pytest-csv can do that.

Install:

pip install pytest-csv

Run:

pytest --csv out.csv

Here is a sample out.csv:

id,module,name,file,doc,markers,status,message,duration
tests/test_my_functions.py::test1,tests.test_my_functions,test1,tests/test_my_functions.py,,,passed,,8.583301678299904e-05
tests/test_my_functions.py::test2,tests.test_my_functions,test2,tests/test_my_functions.py,,,failed,AssertionError: assert foo == 'bar',0.0006891670054756105
Hai Vu
  • 37,849
  • 11
  • 66
  • 93