简体   繁体   中英

Python unit test running error using flask_restful

Following is the directory structure. I am trying to run and i get the following error and below is my test_vuln.py code. The app runs fine by itself and it also runs without any errors when i do 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

How do i run unit tests here, please help. thanks

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

That's because the last lines of code, if __name__ == '__main__': , have the purpose of running testing files like this python tests/test_vuln.py . If you want to run this program python -m unittest tests/test_vuln.py , try erasing the lines above. So:

With:

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

Run the script with the following command: python tests/test_vuln.py . And without it, run the script with the following command: python -m unittest tests/test_vuln.py . Helpful info in link 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM