gpt4 book ai didi

python - 单元测试以检查给定路径是否返回正确的上下文

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:22 25 4
gpt4 key购买 nike

如题。我有一个可以手动测试的模型。我在浏览器中输入 url 并收到其中一个 View 的结果。事情是 unittest 应该这样做。

我认为应该有一些方法来创建请求,将其发送到应用程序并作为返回接收上下文。

最佳答案

您可以使用 WebTest 包创建功能测试,它允许您将 WSGI 应用程序包装在支持 .get().post 的 TestApp 中()

参见 http://docs.pylonsproject.org/projects/pyramid/1.0/narr/testing.html#creating-functional-tests对于 Pyramid 中的细节,粘贴在这里供后代使用:

import unittest

class FunctionalTests(unittest.TestCase):
def setUp(self):
from myapp import main
app = main({})
from webtest import TestApp
self.testapp = TestApp(app)

def test_root(self):
res = self.testapp.get('/', status=200)
self.failUnless('Pyramid' in res.body)

关于python - 单元测试以检查给定路径是否返回正确的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5682581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com