From b8937c031209b9ea1164affc10ab7981215f955a Mon Sep 17 00:00:00 2001 From: Matt Schwager Date: Wed, 12 Dec 2018 07:52:52 -0700 Subject: [PATCH] Switch to using pytest instead of nose2 --- .travis.yml | 3 +- README.md | 5 ++-- requirements-dev.txt | 3 +- tests/test_analytics.py | 23 ++++++++------- tests/test_api.py | 49 ++++++++++++++++---------------- tests/test_output/test_json.py | 8 +++--- tests/test_output/test_stdout.py | 10 +++---- 7 files changed, 53 insertions(+), 48 deletions(-) diff --git a/.travis.yml b/.travis.yml index b6296ef..6695e5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,8 +15,9 @@ before_install: install: - pip install -r requirements.txt - pip install -r requirements-dev.txt + - pip install -e . script: - flake8 - - nose2 --with-coverage + - pytest --cov after_success: - coveralls diff --git a/README.md b/README.md index 807face..1a9e194 100644 --- a/README.md +++ b/README.md @@ -150,12 +150,13 @@ First, install development packages: ``` $ pip install -r requirements-dev.txt +$ pip install -e . ``` ## Testing ``` -$ nose2 +$ pytest ``` ## Linting @@ -167,5 +168,5 @@ $ flake8 ## Coverage ``` -$ nose2 --with-coverage +$ pytest --cov ``` diff --git a/requirements-dev.txt b/requirements-dev.txt index ea61ffe..a8a4a00 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,4 @@ -nose2==0.8.0 +pytest==4.0.1 +pytest-cov==2.6.0 flake8==3.6.0 coveralls==1.5.1 diff --git a/tests/test_analytics.py b/tests/test_analytics.py index 6038b1d..ed55ff0 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -11,6 +11,7 @@ import mock import requests +import pytest from gitem import analytics from gitem import api @@ -55,7 +56,7 @@ def test_get_organization_information(self): ('# of Public Repositories', 'pr1'), ]) - self.assertEqual(result, expected) + assert result == expected def test_get_organization_repositories(self): return_value = [ @@ -98,7 +99,7 @@ def test_get_organization_repositories(self): ]), ] - self.assertEqual(result, expected) + assert result == expected def test_get_organization_members(self): return_value = [ @@ -140,7 +141,7 @@ def test_get_organization_members(self): ]), ] - self.assertEqual(result, expected) + assert result == expected def test_get_repository_information(self): return_value = ( @@ -183,7 +184,7 @@ def test_get_repository_information(self): ('Watchers', 'wc1'), ]) - self.assertEqual(result, expected) + assert result == expected def test_get_repository_contributors(self): return_value = [ @@ -221,7 +222,7 @@ def test_get_repository_contributors(self): ]), ] - self.assertEqual(result, expected) + assert result == expected def test_get_user_information(self): return_value = ( @@ -258,7 +259,7 @@ def test_get_user_information(self): ('Updated', 'ua1'), ]) - self.assertEqual(result, expected) + assert result == expected def test_get_user_organization(self): return_value = [ @@ -292,7 +293,7 @@ def test_get_user_organization(self): ]), ] - self.assertEqual(result, expected) + assert result == expected def test_get_user_repositories(self): return_value = [ @@ -323,7 +324,7 @@ def test_get_user_repositories(self): ]) ] - self.assertEqual(result, expected) + assert result == expected def test_get_repository_commit_emails_basic(self): return_value = [ @@ -349,7 +350,7 @@ def test_get_repository_commit_emails_basic(self): expected = {('username1', 'email1')} - self.assertEqual(result, expected) + assert result == expected def test_get_repository_commit_emails_conflict(self): def conflict_generator(): @@ -367,7 +368,7 @@ def conflict_generator(): expected = set() - self.assertEqual(result, expected) + assert result == expected def test_get_repository_commit_emails_not_conflict(self): def not_conflict_generator(): @@ -381,7 +382,7 @@ def not_conflict_generator(): return_value=return_value ) - with self.assertRaises(api.ApiCallException): + with pytest.raises(api.ApiCallException): analytics.get_repository_commit_emails(ghapi, "unused", "unused") diff --git a/tests/test_api.py b/tests/test_api.py index cafab0d..d97f5b2 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -10,6 +10,7 @@ import mock import requests +import pytest from gitem import api @@ -19,10 +20,10 @@ class TestApi(unittest.TestCase): def assertOk(self, status_code): - self.assertEqual(status_code, requests.codes.OK) + assert status_code == requests.codes.OK def assertEmpty(self, iterable): - self.assertEqual(len(iterable), 0) + assert len(iterable) == 0 @staticmethod def api_will_return(json_return_value, status_code=requests.codes.OK, oauth2_token=None): @@ -82,7 +83,7 @@ def test_ok(self): ) self.assertOk(status_code) - self.assertEqual(result, expected) + assert result == expected def test_invalid_json(self): will_return = mocked_api_results.INVALID_JSON_RESULT @@ -91,10 +92,10 @@ def test_invalid_json(self): # The API call we make doesn't matter, it will return the same result # no matter what - with self.assertRaises(api.ApiCallException) as e: + with pytest.raises(api.ApiCallException) as e: mocked_api.get_public_organization("unused") - self.assertTrue(e.exception.bad_request) + assert e.value.bad_request def test_invalid_json_argument_type(self): will_return = mocked_api_results.BAD_JSON_VALUES_RESULT @@ -103,10 +104,10 @@ def test_invalid_json_argument_type(self): # The API call we make doesn't matter, it will return the same result # no matter what - with self.assertRaises(api.ApiCallException) as e: + with pytest.raises(api.ApiCallException) as e: mocked_api.get_public_organization("unused") - self.assertTrue(e.exception.bad_request) + assert e.value.bad_request def test_invalid_json_field(self): will_return = mocked_api_results.INVALID_FIELDS_RESULT @@ -115,10 +116,10 @@ def test_invalid_json_field(self): # The API call we make doesn't matter, it will return the same result # no matter what - with self.assertRaises(api.ApiCallException) as e: + with pytest.raises(api.ApiCallException) as e: mocked_api.get_public_organization("unused") - self.assertTrue(e.exception.unprocessable_entity) + assert e.value.unprocessable_entity def test_bad_credentials(self): will_return = mocked_api_results.BAD_CREDENTIALS_RESULT @@ -127,10 +128,10 @@ def test_bad_credentials(self): # The API call we make doesn't matter, it will return the same result # no matter what - with self.assertRaises(api.ApiCallException) as e: + with pytest.raises(api.ApiCallException) as e: mocked_api.get_public_organization("unused") - self.assertTrue(e.exception.unauthorized) + assert e.value.unauthorized def test_maximum_bad_credentials(self): will_return = mocked_api_results.MAXIMUM_BAD_CREDENTIALS_RESULT @@ -139,10 +140,10 @@ def test_maximum_bad_credentials(self): # The API call we make doesn't matter, it will return the same result # no matter what - with self.assertRaises(api.ApiCallException) as e: + with pytest.raises(api.ApiCallException) as e: mocked_api.get_public_organization("unused") - self.assertTrue(e.exception.forbidden) + assert e.value.forbidden def test_not_found(self): will_return = mocked_api_results.NOT_FOUND_RESULT @@ -151,10 +152,10 @@ def test_not_found(self): # The API call we make doesn't matter, it will return the same result # no matter what - with self.assertRaises(api.ApiCallException) as e: + with pytest.raises(api.ApiCallException) as e: mocked_api.get_public_organization("unused") - self.assertTrue(e.exception.not_found) + assert e.value.not_found def test_authenticated_endpoint_ok(self): will_return = mocked_api_results.STANDARD_API_RESULT @@ -171,7 +172,7 @@ def test_authenticated_endpoint_ok(self): ) self.assertOk(status_code) - self.assertEqual(result, expected) + assert result == expected def test_authenticated_endpoint_missing_token(self): will_return = mocked_api_results.STANDARD_API_RESULT @@ -181,7 +182,7 @@ def test_authenticated_endpoint_missing_token(self): oauth2_token=None ) - with self.assertRaises(api.AuthenticationRequiredException): + with pytest.raises(api.AuthenticationRequiredException): mocked_api.get_organization("unused") def test_paged_ok(self): @@ -203,7 +204,7 @@ def test_paged_ok(self): ) self.assertOk(status_code) - self.assertEqual(result, expected) + assert result == expected def test_paged_pep_479(self): mocked_json_values = [ @@ -225,35 +226,35 @@ def test_get_users_public_repositories_bad_type(self): type_ = "" ghapi = api.Api() - with self.assertRaises(ValueError): + with pytest.raises(ValueError): ghapi.get_users_public_repositories("UNUSED", type_=type_) def test_get_users_public_repositories_bad_sort(self): sort = "" ghapi = api.Api() - with self.assertRaises(ValueError): + with pytest.raises(ValueError): ghapi.get_users_public_repositories("UNUSED", sort=sort) def test_get_users_public_repositories_bad_direction(self): direction = "" ghapi = api.Api() - with self.assertRaises(ValueError): + with pytest.raises(ValueError): ghapi.get_users_public_repositories("UNUSED", direction=direction) def test_get_organizations_public_repositories_bad_type(self): type_ = "" ghapi = api.Api() - with self.assertRaises(ValueError): + with pytest.raises(ValueError): ghapi.get_organizations_public_repositories("UNUSED", type_=type_) def test_get_repository_contributors_bad_anon(self): anon = "" ghapi = api.Api() - with self.assertRaises(ValueError): + with pytest.raises(ValueError): ghapi.get_repository_contributors("UNUSED", "UNUSED", anon=anon) def test_get_repository_contributors_ok(self): @@ -275,7 +276,7 @@ def test_get_repository_contributors_ok(self): ) self.assertOk(status_code) - self.assertEqual(result, expected) + assert result == expected if __name__ == "__main__": diff --git a/tests/test_output/test_json.py b/tests/test_output/test_json.py index 95a2eab..5db5544 100644 --- a/tests/test_output/test_json.py +++ b/tests/test_output/test_json.py @@ -28,7 +28,7 @@ def test_basic(self): {"key":"value"} ''') - self.assertEqual(result, expected) + assert result == expected def test_list(self): data = collections.OrderedDict([ @@ -46,7 +46,7 @@ def test_list(self): {"key1":{"key2":["value1","value2"]}} ''') - self.assertEqual(result, expected) + assert result == expected def test_recurse(self): data = collections.OrderedDict([ @@ -65,7 +65,7 @@ def test_recurse(self): {"key1":"value1","key2":{"key3":"value2"}} ''') - self.assertEqual(result, expected) + assert result == expected def test_multi(self): data1 = collections.OrderedDict([ @@ -86,7 +86,7 @@ def test_multi(self): {"key2":"value2"} ''') - self.assertEqual(result, expected) + assert result == expected if __name__ == "__main__": diff --git a/tests/test_output/test_stdout.py b/tests/test_output/test_stdout.py index 69d043c..6c6f93b 100644 --- a/tests/test_output/test_stdout.py +++ b/tests/test_output/test_stdout.py @@ -28,7 +28,7 @@ def test_basic(self): key: value ''') - self.assertEqual(result, expected) + assert result == expected def test_list(self): data = collections.OrderedDict([ @@ -49,7 +49,7 @@ def test_list(self): value2 ''') - self.assertEqual(result, expected) + assert result == expected def test_recurse(self): data = collections.OrderedDict([ @@ -71,7 +71,7 @@ def test_recurse(self): key3: value2 ''') - self.assertEqual(result, expected) + assert result == expected def test_newline(self): data = collections.OrderedDict([ @@ -99,7 +99,7 @@ def test_newline(self): key5: value3 ''') - self.assertEqual(result, expected) + assert result == expected def test_multi(self): data1 = collections.OrderedDict([ @@ -120,7 +120,7 @@ def test_multi(self): key2: value2 ''') - self.assertEqual(result, expected) + assert result == expected if __name__ == "__main__":