-
-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
d104d5b
commit 70f6e2f
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
import os | ||
import coverage | ||
from initializer import server | ||
from flask_script import Manager | ||
|
||
manager = Manager(server) | ||
|
||
@manager.command | ||
def test(): | ||
"""Runs the unit tests without coverage.""" | ||
tests = unittest.TestLoader().discover('tests') | ||
result = unittest.TextTestRunner(verbosity=2).run(tests) | ||
if result.wasSuccessful(): | ||
return 0 | ||
else: | ||
sys.exit("Tests have failed") | ||
|
||
|
||
@manager.command | ||
def cov(): | ||
"""Runs the unit tests with coverage.""" | ||
cov = coverage.coverage( | ||
branch=True, | ||
include='./*' | ||
) | ||
cov.start() | ||
tests = unittest.TestLoader().discover('tests') | ||
result = unittest.TextTestRunner(verbosity=2).run(tests) | ||
cov.stop() | ||
cov.save() | ||
print('Coverage Summary:') | ||
cov.report() | ||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
covdir = os.path.join(basedir, 'coverage') | ||
cov.html_report(directory=covdir) | ||
cov.erase() | ||
if result.wasSuccessful(): | ||
return 0 | ||
else: | ||
sys.exit("Tests have failed") | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
manager.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import unittest | ||
import requests | ||
|
||
headers = { | ||
'Host': 'localhost:5000', | ||
'Accept': 'application/json, text/plain, */*', | ||
'Accept-Language': 'en-US,en;q=0.5', | ||
'Accept-Encoding': 'gzip, deflate', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Origin': 'http://localhost:3000' | ||
} | ||
correct_username="rand" | ||
correct_password="pass" | ||
incorrect_username="admin" | ||
incorrect_password="admin" | ||
correct_string="user_name="+correct_username+"&password="+correct_password | ||
incorrect_string="user_name="+incorrect_username+"&password="+incorrect_password | ||
payload = {''} | ||
class TestFlaskAPIUsingRequests(unittest.TestCase): | ||
def test_api_login_returns_auth_level(self): | ||
resp = requests.post('http://localhost:5000/api/login',correct_string,headers=headers) | ||
self.assertEqual(resp.json(),{u'auth': u'0'}) | ||
def test_api_login_incorrectly_return_403(self): | ||
resp = requests.post('http://localhost:5000/api/login',incorrect_string,headers=headers) | ||
self.assertEqual(resp.status_code,403) | ||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
from UserManager import * | ||
import unittest | ||
|
||
|
||
def username(): | ||
return 'admin' | ||
|
||
|
||
def password(): | ||
return 'admin' | ||
|
||
|
||
def usernamer(): | ||
return 'rand' | ||
|
||
|
||
def passwordr(): | ||
return 'pass' | ||
|
||
|
||
class Test(unittest.TestCase): | ||
|
||
def test_incorrect_login(self): | ||
self.assertEqual(False, user_login(username(), password())) | ||
|
||
def test_correct_login(self): | ||
self.assertEqual(True, user_login(usernamer(), passwordr())) | ||
|
||
def test_incorrect_check_approved(self): | ||
self.assertEqual(False, check_approved(username(), password())) | ||
|
||
def test_correct_check_approved(self): | ||
self.assertEqual(True, check_approved(usernamer(), passwordr())) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |