为什么我期望状态代码为200,而在python中进行单元测试时状态代码为404?

为什么我期望状态代码为200,而在python中进行单元测试时状态代码为404?,python,unit-testing,flask,python-unittest,Python,Unit Testing,Flask,Python Unittest,我正在用python为一个文件使用unittest和pytest编写一个单元测试用例。在许多函数中,我无法理解为什么它不能作为一个成功通过的测试用例执行。 这是一个显示设置页面的函数,调用成功后,它将显示一个html页面 @app.route('/settings', methods=['GET', 'POST']) @login_required def settings(): global application_inst if request.method == 'POST

我正在用python为一个文件使用unittest和pytest编写一个单元测试用例。在许多函数中,我无法理解为什么它不能作为一个成功通过的测试用例执行。 这是一个显示设置页面的函数,调用成功后,它将显示一个html页面

@app.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
    global application_inst
    if request.method == 'POST':
        print("Setting changed")

    return render_template('settings.html', user=session['user'], application=application_inst)

函数的单元测试用例如下所示:

class MyApp(unittest.TestCase):
    def setUp(self):
        self.app = create_app(db)
        self.client = self.app.test_client(self)

        with self.app.app_context():
            # create all tables
            db.create_all()

    def tearDown(self):
        pass

    def test_settings_passed(self):
        response = self.client.get('/settings', follow_redirects=True)
        self.assertEqual(response.status_code, 200)
我在stacktrace中得到的错误是:

test_app.py::MyApp::test_settings_passed FAILED                          [100%]ENV :default
############ INIT ############
############ INIT ############
############ INIT ############

 
200 != 404

Expected :404
Actual   :200
<Click to see difference>
请在这方面帮助我。

尝试更改,以便在应用程序上下文中执行self.client,例如

类MyAppunittest.TestCase: def设置自我: self.app=创建_appdb self.client=self.app.test\u clientself 使用self.app.app_上下文: 创建所有表 db.create_all def自动拆卸: 通过 def测试设置通过自身: 使用self.app.app_上下文: response=self.client.get'/settings',follow\u redirects=True self.assertEqualresponse.status_代码,200 我已经创建了相同的场景,它可以与app_上下文一起工作,也可以不与app_上下文一起工作。我还使用此命令执行测试:

python-m unittest tests/test_the_test.py 这还取决于你如何设置应用程序。 使用python3.7,例如bellow

我的是这样的:

├── app.py
├── index
│   ├── __init__.py
│   └── routes.py
└── tests
    └── test_the_test.py
我的app.py如下所示:

├── app.py
├── index
│   ├── __init__.py
│   └── routes.py
└── tests
    └── test_the_test.py
从烧瓶进口烧瓶 def创建应用程序: app=烧瓶名称__ 从索引导入bp app.register\u蓝图bp 返回应用程序 我的索引/\uuuu init\uuuuu.py如下所示:

├── app.py
├── index
│   ├── __init__.py
│   └── routes.py
└── tests
    └── test_the_test.py
从烧瓶导入蓝图 bp=蓝图名称“/” 从索引导入路由 我的index/routes.py如下所示:

├── app.py
├── index
│   ├── __init__.py
│   └── routes.py
└── tests
    └── test_the_test.py
从索引导入bp 从flask导入jsonify @bp.路线“/测试” def测试: 打印“在这里吗?” 返回jsonify{'result':True} 我的测试/test__test.py如下所示:

├── app.py
├── index
│   ├── __init__.py
│   └── routes.py
└── tests
    └── test_the_test.py
导入单元测试 从应用程序导入创建应用程序 类MyAppunittest.TestCase: def设置自我: self.app=创建应用程序 self.client=self.app.test\u clientself def自动拆卸: 通过 def测试设置通过自身: response=self.client.get'/test',follow\u redirects=True self.assertEqualresponse.status_代码,200 def测试设置失败自身: response=self.client.get'/test\u not\u exist',follow\u redirects=True self.assertEqualresponse.status_代码,404 执行此命令:

python-m unittest tests/test_the_test.py 我的设置工作正常,进行测试,并在端点存在和不存在时给出正确的结果

结果:

python -m unittest tests/test_the_test.py 
.goes here?
.
----------------------------------------------------------------------
Ran 2 tests in 0.006s

OK


您正在两次比较相同的状态代码,第二次希望得到不同的输出?还有pytest.raiseAssertionError在这一点上没有意义,也许你想做其他事情?顺便说一句,不要混合使用unittest和pytest,有些可能有效,有些则不行。即使我在收到相同的错误后检查了相同的状态代码,也要坚持使用其中的一个。我想看看我的呼叫/设置是否为我提供了200的状态码@Beanbremenah先生好的,你在assertEqual中有预期的和实际的参数,我没有注意到。所以你期望200,但得到404。你是对的!为什么会以这种方式发生@MrBeanBremen@SimeonNedkov运行时错误:应用程序无法为独立于请求的URL生成创建URL适配器。您可以通过设置SERVER_NAME config变量来解决此问题。感谢您为解决此问题所做的努力,尽管我们的项目结构和代码是相同的,但python versionmine是3.6,我的问题仍然存在。不过我会接受你的回答。基本上404错误意味着找不到该页面,你确定“/settings”是正确的端点吗?因为如果self.client无法访问该应用程序,您将收到另一条消息。可能端点“/settings”的调用不正确,可能它的启动方式不同,例如“/some/structure/settings”?@quirry_guy,你试过我的例子吗?只需设置并查看它是否工作,这意味着这可能是由于您的应用程序的设置方式,可能是上面提到的端点问题?是的@simkus我尝试过您的,它工作正常。我将不得不看看我的应用程序是如何设置的,你的回答对我来说确实是一线希望,非常感谢!