使用 flask_restful 的 Python 单元测试运行错误



以下是目录结构。我正在尝试运行,但出现以下错误,下面是我的test_vuln.py代码。该应用程序本身运行良好,当我进行python测试/test_vuln.py时,它也可以运行没有任何错误。

python -m unittest tests/test_vuln.py 
/Users/skdandavathi/.local/share/virtualenvs/vuln-GqWMRh7_/bin/python: No module named python -m unittest .
vuln
├── Pipfile
├── Pipfile.lock
├── README.md
├── client.py
├── main.py
├── requirements.txt
├── resources
│   ├── api.py
│   ├── errors.py
│   └── routes.py
├── static
│   ├── cve.csv
│   ├── example_expected_results.json
│   ├── example_post_data.json
│   └── out.json
└── tests
├── __init__.py
└── test_vuln.py

我如何在这里运行单元测试,请帮忙。

谢谢
import unittest
import json
import sys
import os
sys.path.append("/Users/skdandavathi/Documents/Karthik/projects/vuln")
from main import app
class TestVulnApi(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
print(self.app)
def test_get_error(self):
response = self.app.get(ENDPOINT)
data = json.loads(response.get_data())
self.assertEqual(response.status_code, 401)

def tearDown(self):
# delete anything above
pass

那是因为最后几行代码,if __name__ == '__main__':, 目的是运行这样的测试文件python tests/test_vuln.py. 如果要python -m unittest tests/test_vuln.py运行此程序,请尝试擦除上面的行。所以:

跟:

if __name__ == '__main__':
unittest.main()

使用以下命令运行脚本:python tests/test_vuln.py。如果没有它,请使用以下命令运行脚本:python -m unittest tests/test_vuln.py.链接 1 中的有用信息。

最新更新