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
Unit testing 设置烧瓶测试客户端_Unit Testing_Flask - Fatal编程技术网

Unit testing 设置烧瓶测试客户端

Unit testing 设置烧瓶测试客户端,unit-testing,flask,Unit Testing,Flask,我正在尝试测试我的flask应用程序,但出现此错误 运行时错误:在应用程序上下文之外工作。 这通常意味着您试图使用所需的功能 以某种方式与当前应用程序对象接口。解决 这将使用app.app_context()设置应用程序上下文。见 有关更多信息,请参阅文档` 我已经试着理解错误,但我所知道的是,有一个客户端实例应该被实例化以用于测试。救命啊。 我的代码: import unittest from flask import jsonify class TestAuth(unittest.Te

我正在尝试测试我的flask应用程序,但出现此错误

运行时错误:在应用程序上下文之外工作。 这通常意味着您试图使用所需的功能 以某种方式与当前应用程序对象接口。解决 这将使用app.app_context()设置应用程序上下文。见 有关更多信息,请参阅文档`

我已经试着理解错误,但我所知道的是,有一个客户端实例应该被实例化以用于测试。救命啊。 我的代码:

import unittest

from flask import jsonify


class TestAuth(unittest.TestCase):
"""Class for testing all the API endpoints"""
def setUp(self):
    """Initializing a test client and making the environment a testing one"""
    app.app.config['TESTING'] = True
    self.app = app.app.test_client()
    self.app.testing = True

def sign_in(self, email='user@gmail.com', password='testpass'):
    user_data = jsonify({"email": email, "password": password})
    return self.app.post('/api/v1/auth/signup/', data=user_data)

def log_in(self, email='user@gmail.com', password='testpass'):
    user_data = jsonify({"email": email, "password": password})
    return self.app.post('/api/v1/auth/login/', data=user_data)

def test_home_status_code(self):

    result = self.app.get('/api/v1/')
    self.assertEqual(result.status_code, 200)

def test_signin_status_code(self):
    result = self.sign_in()
    self.assertEqual(result.status_code, 200)

def test_login_correct_login(self):
    """test login after signing in"""
    self.sign_in()
    result = self.log_in()
    self.assertEqual(result.status_code, 200)
    self.assertIn(b'Success', result.message)

def test_login_with_wrong_credentials(self):
    """test successful login"""
    self.sign_in()  # must sign in first for successful login
    result = self.log_in(email='wrong@mail', password='wrongpass')
    self.assertIn(b'Wrong Username or Password', result.message)


if __name__ == "__main__":
unittest.main()
试试这个:

def test_home_status_code(self):

    with self.app as client:
        result = client.get('/api/v1/')
        self.assertEqual(result.status_code, 200)