Search code examples
pythoncode-coveragetornadoyieldcoroutine

How to do coverage on a python coroutine?


I am using tornado coroutines in python 2.7 and I have done unit tests like this one:

def test_my_coroutine_function(self):
    # Arranges
    ...

    # Acts
    response = yield my_function()

    # Asserts
    ...

My function is defined like that:

@tornado.gen.coroutine
def my_function(self):
    a = True

My issue is that coverage.py tell me that the line "a = True" is not covered.

To use coverage, I ran the command line below:

coverage run -m --source=./ unittest discover ./; coverage html;

Thank you for your help.


Solution

  • Ok I figured out how to make it works.

    I just have have to replace my unit test by the following:

    def test_my_coroutine_function(self):
        # Arranges
        ...
    
        # Acts
        future_response= yield my_function()
        response = future_response.result()
    
        # Asserts
        ...
    

    That's it.