Pytests on web.py application not covering methods code

帅比萌擦擦* 提交于 2020-03-22 09:27:30

问题


First of all, sorry if the linguo is not 100% correct or something does not make 100% of sense, I am quite new into web aplication development and posting on stack overflow in general.

I have a web.py application and need to test its functionalities with pytest and generate a code coverage report with pytest-cov. I get the tests to work and assert on the responses, but when I generate the code report, all the lines of code inside the methods are uncovered and therefore get a really low test coverage (23%)

I am successfully running pytest --cov in my repository via cmd and getting the coverage result in it. But then I tried using coverage run -m pytest test_Server.py and running coverage report to get a bit more detail.

In this report is where I could see which lines of code I was missing, and I was missing all of them except the definition of each method/class.

Another thing I tried is pytest --cov=Server.py, which then gives me the error

Coverage.py warning: Module Server.py was never imported. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
WARNING: Failed to generate report: No data to report.

Server.py

import os

import web


URLS = ("/", "Index")

APP = web.application(URLS, globals())


class Index:
    """
    Just a test echo server.
    """

    def POST(self):
        web.header("Access-Control-Allow-Origin", "*")
        data = web.data()

        return data

test_Server.py


from paste.fixture import TestApp
import pytest
import os
import sys

sys.path.insert(1,(os.path.join(sys.path[0],'..'))) #adding parent path to import server script
import Server as lm
from Server import APP as app, Index

host = "localHost:9999"


class TestRig():
    def test_server_setup(self):
        middleware = []
        testApp = TestApp(app.wsgifunc(*middleware))
        try:
            r = testApp.post("http://%s/" %host)
            print ("request:", r.status)
            assert r.status ==  200

        except TypeError:
            print ("Request failed. Status:"+ r.status)
            raise

This is a very simplifyed version of what I am currently running, and one way or another I manage to get the tests working and assert the responses correctly.

What I would expect is for the code inside the methods to be covered with the tests, but the actual output tells me only the definition of the method is covered and nothing else.


回答1:


Thanks to @hoefling I figured out two things:

  1. When using Web.py, you need to use paste.fixture library to test the code, requests library WON'T cover your code even though you can assert and use get/post methods (here is why I got such a bad code coverage in the beginning).

  2. When using pytest-cov (or coverage.py), make the call as such: pytest --cov=name_of_your_script_to_cover --cov-report=term-missing --cov-report=html to avoid the Failed to generate report: No data to report. error (and additionally get a nice HTML report to view your code coverage).



来源:https://stackoverflow.com/questions/56293883/pytests-on-web-py-application-not-covering-methods-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!