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 单元测试flask应用程序,但避免调用其他方法_Python_Unit Testing_Flask - Fatal编程技术网

Python 单元测试flask应用程序,但避免调用其他方法

Python 单元测试flask应用程序,但避免调用其他方法,python,unit-testing,flask,Python,Unit Testing,Flask,我有一个小型Python(Flask)应用程序,它正在8080端口监听POST请求。 我正在写一些单元测试,但我想知道如何避免调用另一个方法,例如在我的单元测试中的第11行。我只想能够测试后,并验证响应代码是200,如果正确的信息传递 main.py 1 @app.route("/endpoint/", methods=['POST']) 2 def mypostmethod(): 3 4 if 'Content-Type' in request.headers and request.h

我有一个小型Python(Flask)应用程序,它正在8080端口监听POST请求。 我正在写一些单元测试,但我想知道如何避免调用另一个方法,例如在我的单元测试中的第11行。我只想能够测试后,并验证响应代码是200,如果正确的信息传递

main.py

1 @app.route("/endpoint/", methods=['POST'])
2 def mypostmethod():
3
4    if 'Content-Type' in request.headers and request.headers['Content-Type'] == 'application/json':
5        post_data = request.json
6
7        req_data = request.get_json()
8        content = req_data['content']
9
10       #####--How to avoid this in unit test--start--
11       result = someClass.someMethod(content)
12       #####--How to avoid this in unit test--end--
13
14       return content
15
16   else:
17   if not 'Content-Type' in request.headers:
18       return Response("No Content-Type set in request header", status='400')
19
20   if request.headers['Content-Type'] != 'application/json':
21       return Response("Content-Type in request header should be application/json", status='400')
import json
import main
import unittest

content = 'hello'

class PublicTestCase(unittest.TestCase):
    def setUp(self):
        self.app = main.app.test_client()

    def test_send_valid_request(self):
        headers = {
            'Content-Type': 'application/json'
        }
        request_body = {
            'content': content
        }

        response = self.app.post('/endpoint',
                                 headers=headers,
                                 data=json.dumps(request_body),
                                 follow_redirects=True)

        assert response.status_code == 200
我的单元测试如下所示:

test\u main.py

1 @app.route("/endpoint/", methods=['POST'])
2 def mypostmethod():
3
4    if 'Content-Type' in request.headers and request.headers['Content-Type'] == 'application/json':
5        post_data = request.json
6
7        req_data = request.get_json()
8        content = req_data['content']
9
10       #####--How to avoid this in unit test--start--
11       result = someClass.someMethod(content)
12       #####--How to avoid this in unit test--end--
13
14       return content
15
16   else:
17   if not 'Content-Type' in request.headers:
18       return Response("No Content-Type set in request header", status='400')
19
20   if request.headers['Content-Type'] != 'application/json':
21       return Response("Content-Type in request header should be application/json", status='400')
import json
import main
import unittest

content = 'hello'

class PublicTestCase(unittest.TestCase):
    def setUp(self):
        self.app = main.app.test_client()

    def test_send_valid_request(self):
        headers = {
            'Content-Type': 'application/json'
        }
        request_body = {
            'content': content
        }

        response = self.app.post('/endpoint',
                                 headers=headers,
                                 data=json.dumps(request_body),
                                 follow_redirects=True)

        assert response.status_code == 200

我最近使用这种方法,仅当pytest不是sys.argv时才触发该方法。但它也可以与unittest一起使用。
像这样

10、如何在单元测试中避免这种情况——开始--
-10如果没有任何(['unittest'在arg中表示sys.argv中的arg]):
-10#运行cmd pytest时,eval为false
11结果=someClass.someMethod(内容)
12、单元测试中如何避免这种情况——结束--

另一种方法是在运行时在设备上设置一个env var
希望这能有所帮助
顺便说一句,这段代码并没有经过测试,所以可能会有语法错误,这只是给你们一些想法