Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pytest为web.py应用程序编写单元测试_Python_Unit Testing_Pytest_Web.py - Fatal编程技术网

Python 使用pytest为web.py应用程序编写单元测试

Python 使用pytest为web.py应用程序编写单元测试,python,unit-testing,pytest,web.py,Python,Unit Testing,Pytest,Web.py,我想使用pytest为web.py应用程序编写单元测试。如何在pytest中调用web.py服务 代码: 这可以通过使用python请求模块来完成,当我们运行web.py服务时,它将运行。然后导入请求模块并使用get方法和响应对象,您可以验证结果。那很好 通过使用Paste和nose,我们也可以根据web.py官方文档实现这一点 pytest中是否有类似于paste和nose选项的解决方案。是。实际上,web.py配方中的代码几乎可以按原样与py.test一起使用,只需删除nose.toolsi

我想使用pytest为web.py应用程序编写单元测试。如何在pytest中调用web.py服务

代码:

这可以通过使用python请求模块来完成,当我们运行web.py服务时,它将运行。然后导入请求模块并使用get方法和响应对象,您可以验证结果。那很好

通过使用Paste和nose,我们也可以根据web.py官方文档实现这一点


pytest中是否有类似于paste和nose选项的解决方案。

是。实际上,web.py配方中的代码几乎可以按原样与py.test一起使用,只需删除
nose.tools
import并适当更新断言即可

但是,如果您想知道如何以py.test样式为web.py应用程序编写测试,它们可以如下所示:

从paste.fixture导入TestApp
#我假设问题中的代码保存在名为app.py的文件中,
#在与测试相同的目录中。我正在从该文件导入变量“app”
从应用程序导入应用程序
def测试索引():
中间件=[]
test_app=TestApp(app.wsgifunc(*中间件))
r=测试应用程序获取('/'))
断言r.status==200
断言“你好,世界!”在r
在添加进一步的测试时,您可能会将测试应用程序的创建重构到一个fixture中:

添加了pytest导入夹具的

从paste.fixture导入TestApp
从应用程序导入应用程序
def测试索引(测试应用程序):
r=测试应用程序获取('/'))
断言r.status==200
断言“你好,世界!”在r
@固定装置()
def测试应用程序():
中间件=[]
返回TestApp(app.wsgifunc(*中间件))

是。实际上,web.py配方中的代码几乎可以按原样与py.test一起使用,只需删除
nose.tools
import并适当更新断言即可

但是,如果您想知道如何以py.test样式为web.py应用程序编写测试,它们可以如下所示:

从paste.fixture导入TestApp
#我假设问题中的代码保存在名为app.py的文件中,
#在与测试相同的目录中。我正在从该文件导入变量“app”
从应用程序导入应用程序
def测试索引():
中间件=[]
test_app=TestApp(app.wsgifunc(*中间件))
r=测试应用程序获取('/'))
断言r.status==200
断言“你好,世界!”在r
在添加进一步的测试时,您可能会将测试应用程序的创建重构到一个fixture中:

添加了pytest导入夹具的

从paste.fixture导入TestApp
从应用程序导入应用程序
def测试索引(测试应用程序):
r=测试应用程序获取('/'))
断言r.status==200
断言“你好,世界!”在r
@固定装置()
def测试应用程序():
中间件=[]
返回TestApp(app.wsgifunc(*中间件))

感谢您的回复,我们将尝试此解决方案并让您知道结果感谢您的回复,我们将尝试此解决方案并让您知道结果
import web

urls = (
    '/', 'index'
)

app = web.application(urls, globals()) 

class index:
    def GET(self):
        return "Hello, world!"

if __name__ == "__main__":    
 app.run()