web.py 应用程序上的 Pytests 不包括方法代码 [英] Pytests on web.py application not covering methods code

查看:51
本文介绍了web.py 应用程序上的 Pytests 不包括方法代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,抱歉,如果语言不是 100% 正确或某些东西不是 100% 有意义,我对 Web 应用程序开发和一般堆栈溢出问题还很陌生.

我有一个 web.py 应用程序,需要使用 pytest 测试其功能并使用 pytest-cov 生成代码覆盖率报告.我让测试工作并对响应进行断言,但是当我生成代码报告时,方法中的所有代码行都没有覆盖,因此测试覆盖率非常低 (23%)

我通过 cmd 在我的存储库中成功运行 pytest --cov 并在其中获取覆盖结果.但后来我尝试使用 coverage run -m pytest test_Server.py 并运行 coverage report 以获取更多详细信息.

在这份报告中,我可以看到我遗漏了哪些代码行,除了每个方法/类的定义之外,我遗漏了所有代码行.

我尝试的另一件事是 pytest --cov=Server.py,然后给我错误

Coverage.py 警告:Module Server.py 从未被导入.(模块未导入)Coverage.py 警告:未收集数据.(未收集数据)警告:无法生成报告:没有要报告的数据.

服务器.py

导入操作系统导入网页URLS = ("/", "索引")APP = web.application(URLS, globals())班级索引:"""只是一个测试回声服务器."""def POST(自我):web.header("Access-Control-Allow-Origin", "*")数据 = web.data()返回数据

test_Server.py

<预><代码>从 paste.fixture 导入 TestApp导入pytest导入操作系统导入系统sys.path.insert(1,(os.path.join(sys.path[0],'..'))) #添加父路径导入服务器脚本导入服务器为 lm从服务器导入应用程序作为应用程序,索引主机=本地主机:9999"类 TestRig():def test_server_setup(self):中间件 = []testApp = TestApp(app.wsgifunc(*middleware))尝试:r = testApp.post("http://%s/" %host)打印(请求:",r.status)断言 r.status == 200除了类型错误:打印(请求失败.状态:"+ r.status)增加

这是我当前运行的一个非常简化的版本,我设法以一种或另一种方式使测试正常工作并正确断言响应.

我期望测试覆盖方法中的代码,但实际输出告诉我只覆盖了方法的定义,没有其他任何内容.

解决方案

感谢 @hoefling 我发现了两件事:

  1. 使用Web.py时,需要使用paste.fixture库来测试代码,requests库WON'即使您可以断言和使用 get/post 方法,也要覆盖您的代码(这就是为什么我一开始的代码覆盖率如此糟糕).

  2. 当使用 pytest-cov(或coverage.py)时,调用如下:pytest --cov=name_of_your_script_to_cover --cov-report=term-missing --cov-report=html 以避免 Failed to generate report: No data to report. 错误(另外获得一个不错的 HTML 报告来查看您的代码覆盖率).

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.

解决方案

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).

这篇关于web.py 应用程序上的 Pytests 不包括方法代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆