以下是目录结构。我正在尝试运行,但出现以下错误,下面是我的test_vuln.py代码。该应用程序本身运行良好,当我执行python tests / 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
。 link 1中的有用信息。