-
-
Notifications
You must be signed in to change notification settings - Fork 494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added test coverage and renamed existing test files to allow coverage to work #886
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new line at the end of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay i will |
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() |
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 | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why there are two blank lines between code blocks instead of one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No these are left because of keeping the code easy to read but if you think i will remove the extra line. |
||
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this file do? is it a script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file adds a command for running the tests without the coverage and with the coverge the respective commands being
python TestCommands.py test
andpython TestCommands.py cov