Skip to content

Commit 70f6e2f

Browse files
committed
add test coverage for existing tests and add extra command to run all the tests at once and renamed the existing test files for coverage to work
1 parent d104d5b commit 70f6e2f

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

components/core/TestCommands.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
import os
3+
import coverage
4+
from initializer import server
5+
from flask_script import Manager
6+
7+
manager = Manager(server)
8+
9+
@manager.command
10+
def test():
11+
"""Runs the unit tests without coverage."""
12+
tests = unittest.TestLoader().discover('tests')
13+
result = unittest.TextTestRunner(verbosity=2).run(tests)
14+
if result.wasSuccessful():
15+
return 0
16+
else:
17+
sys.exit("Tests have failed")
18+
19+
20+
@manager.command
21+
def cov():
22+
"""Runs the unit tests with coverage."""
23+
cov = coverage.coverage(
24+
branch=True,
25+
include='./*'
26+
)
27+
cov.start()
28+
tests = unittest.TestLoader().discover('tests')
29+
result = unittest.TextTestRunner(verbosity=2).run(tests)
30+
cov.stop()
31+
cov.save()
32+
print('Coverage Summary:')
33+
cov.report()
34+
basedir = os.path.abspath(os.path.dirname(__file__))
35+
covdir = os.path.join(basedir, 'coverage')
36+
cov.html_report(directory=covdir)
37+
cov.erase()
38+
if result.wasSuccessful():
39+
return 0
40+
else:
41+
sys.exit("Tests have failed")
42+
43+
44+
45+
if __name__ == '__main__':
46+
manager.run()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
import requests
3+
4+
headers = {
5+
'Host': 'localhost:5000',
6+
'Accept': 'application/json, text/plain, */*',
7+
'Accept-Language': 'en-US,en;q=0.5',
8+
'Accept-Encoding': 'gzip, deflate',
9+
'Content-Type': 'application/x-www-form-urlencoded',
10+
'Origin': 'http://localhost:3000'
11+
}
12+
correct_username="rand"
13+
correct_password="pass"
14+
incorrect_username="admin"
15+
incorrect_password="admin"
16+
correct_string="user_name="+correct_username+"&password="+correct_password
17+
incorrect_string="user_name="+incorrect_username+"&password="+incorrect_password
18+
payload = {''}
19+
class TestFlaskAPIUsingRequests(unittest.TestCase):
20+
def test_api_login_returns_auth_level(self):
21+
resp = requests.post('http://localhost:5000/api/login',correct_string,headers=headers)
22+
self.assertEqual(resp.json(),{u'auth': u'0'})
23+
def test_api_login_incorrectly_return_403(self):
24+
resp = requests.post('http://localhost:5000/api/login',incorrect_string,headers=headers)
25+
self.assertEqual(resp.status_code,403)
26+
if __name__ == "__main__":
27+
unittest.main()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
from UserManager import *
4+
import unittest
5+
6+
7+
def username():
8+
return 'admin'
9+
10+
11+
def password():
12+
return 'admin'
13+
14+
15+
def usernamer():
16+
return 'rand'
17+
18+
19+
def passwordr():
20+
return 'pass'
21+
22+
23+
class Test(unittest.TestCase):
24+
25+
def test_incorrect_login(self):
26+
self.assertEqual(False, user_login(username(), password()))
27+
28+
def test_correct_login(self):
29+
self.assertEqual(True, user_login(usernamer(), passwordr()))
30+
31+
def test_incorrect_check_approved(self):
32+
self.assertEqual(False, check_approved(username(), password()))
33+
34+
def test_correct_check_approved(self):
35+
self.assertEqual(True, check_approved(usernamer(), passwordr()))
36+
37+
if __name__ == "__main__":
38+
unittest.main()

0 commit comments

Comments
 (0)