为什么我期望状态代码为 200,但在 python 中进行单元测试时得到的状态代码为 404?
Why am I expecting a status code of 200 but got a status code of 404 while unit testing in python?
我正在 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)
我在堆栈跟踪中得到的错误是:
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
将在 app_context()
内执行,例如
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):
with self.app.app_context():
response = self.client.get('/settings', follow_redirects=True)
self.assertEqual(response.status_code, 200)
我创建了相同的场景,无论是否使用 app_context
,它都适用于我。我也用这个命令来执行测试:
python -m unittest tests/test_the_test.py
另外,这取决于您如何设置应用程序。
例如下面使用 python3.7
。
我的是这样的:
├── app.py
├── index
│ ├── __init__.py
│ └── routes.py
└── tests
└── test_the_test.py
我的app.py
是这样的:
from flask import Flask
def create_app():
app = Flask(__name__)
from index import bp
app.register_blueprint(bp)
return app
我的index/__init__.py
是这样的:
from flask import Blueprint
bp = Blueprint(__name__, '/')
from index import routes
我的 index/routes.py
是这样的:
from index import bp
from flask import jsonify
@bp.route('/test')
def test():
print('goes here?')
return jsonify({'result': True})
我的 tests/test_the_test.py
是这样的:
import unittest
from app import create_app
class MyApp(unittest.TestCase):
def setUp(self):
self.app = create_app()
self.client = self.app.test_client(self)
def tearDown(self):
pass
def test_settings_passed(self):
response = self.client.get('/test', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_settings_failed(self):
response = self.client.get('/test_not_exist', follow_redirects=True)
self.assertEqual(response.status_code, 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
我正在 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)
我在堆栈跟踪中得到的错误是:
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
将在 app_context()
内执行,例如
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):
with self.app.app_context():
response = self.client.get('/settings', follow_redirects=True)
self.assertEqual(response.status_code, 200)
我创建了相同的场景,无论是否使用 app_context
,它都适用于我。我也用这个命令来执行测试:
python -m unittest tests/test_the_test.py
另外,这取决于您如何设置应用程序。
例如下面使用 python3.7
。
我的是这样的:
├── app.py
├── index
│ ├── __init__.py
│ └── routes.py
└── tests
└── test_the_test.py
我的app.py
是这样的:
from flask import Flask
def create_app():
app = Flask(__name__)
from index import bp
app.register_blueprint(bp)
return app
我的index/__init__.py
是这样的:
from flask import Blueprint
bp = Blueprint(__name__, '/')
from index import routes
我的 index/routes.py
是这样的:
from index import bp
from flask import jsonify
@bp.route('/test')
def test():
print('goes here?')
return jsonify({'result': True})
我的 tests/test_the_test.py
是这样的:
import unittest
from app import create_app
class MyApp(unittest.TestCase):
def setUp(self):
self.app = create_app()
self.client = self.app.test_client(self)
def tearDown(self):
pass
def test_settings_passed(self):
response = self.client.get('/test', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_settings_failed(self):
response = self.client.get('/test_not_exist', follow_redirects=True)
self.assertEqual(response.status_code, 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