pytest implementing a logfile per test method
<p>I would like to create a separate log file for each test method. And i would like to do this in the conftest.py file and pass the logfile instance to the test method. This way, whenever i log something in a test method it would log to a separate log file and will be very easy to analyse.</p>

<p>I tried the following.
Inside conftest.py file i added this:</p>

<pre><code>logs_dir = pkg_resources.resource_filename("test_results", "logs")
def pytest_runtest_setup(item):
    test_method_name = item.name
    testpath = item.parent.name.strip('.py')
    path = '%s/%s' % (logs_dir, testpath)
    if not os.path.exists(path):
        os.makedirs(path)
    log = logger.make_logger(test_method_name, path) # Make logger takes care of creating the logfile and returns the python logging object.
</code></pre>

<p>The problem here is that pytest_runtest_setup does not have the ability to return anything to the test method. Atleast, i am not aware of it.</p>

<p>So, i thought of creating a fixture method inside the conftest.py file with scope="function" and call this fixture from the test methods. But, the fixture method does not know about the the Pytest.Item object. In case of pytest_runtest_setup method, it receives the item parameter and using that we are able to find out the test method name and test method path.</p>

<p>Please help! </p>