How to save pytest's results/logs to a file?
Question: I am having trouble trying to save -all- of the results shown from pytest to a
file (txt, log, doesn't matter). In the test example below, I would like to
capture what is shown in console into a text/log file of some sort:

    
    
    import pytest
    import os
    
    def test_func1():
        assert True
    
    
    def test_func2():
        assert 0 == 1
    
    if __name__ == '__main__':
    
        pytest.main(args=['-sv', os.path.abspath(__file__)])
    

Console output I'd like to save to a text file:

    
    
    test-mbp:hi_world ua$ python test_out.py
    ================================================= test session starts =================================================
    platform darwin -- Python 2.7.6 -- py-1.4.28 -- pytest-2.7.1 -- /usr/bin/python
    rootdir: /Users/tester/PycharmProjects/hi_world, inifile: 
    plugins: capturelog
    collected 2 items 
    
    test_out.py::test_func1 PASSED
    test_out.py::test_func2 FAILED
    
    ====================================================== FAILURES =======================================================
    _____________________________________________________ test_func2 ______________________________________________________
    
        def test_func2():
    >       assert 0 == 1
    E       assert 0 == 1
    
    test_out.py:9: AssertionError
    ========================================= 1 failed, 1 passed in 0.01 seconds ==========================================
    test-mbp:hi_world ua$ 
    


Answer: It appears that all of your test output is going _stdout_ , so you simply need
to “redirect” your python invocation's output there:

    
    
    python test_out.py >myoutput.log
    

You can also “tee” the output to multiple places. E.g., you might want to log
to the file yet also see the output on your console. The above example then
becomes:

    
    
    python test_out.py | tee myoutput.log
    

