Skip to content

Commit

Permalink
Migrate tests to pytest #198 (#202)
Browse files Browse the repository at this point in the history
* Migrate tests to pytest

* Update requirements-dev.txt

Co-authored-by: Michael <michael@viz.ai>
  • Loading branch information
1 parent 19e7556 commit 51c3f6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
4 changes: 3 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-r requirements.txt
tox==3.20.0
coverage==5.3
pylint==2.6.0
pylint==2.6.0
pytest-cov==2.10.1
pylint==2.6.0
51 changes: 24 additions & 27 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json
import os
import unittest
import pytest
from typing import Dict, List, Union, Text

from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT
from pyms.flask.app import config

Expand All @@ -18,42 +17,40 @@ def _format_response(response: Text = "") -> Union[List, Dict]:
return json.loads(response)


class ProjectTestCase(unittest.TestCase):
class TestProject:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

def setUp(self):
@pytest.fixture(scope="session")
def microservice(self):
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(self.BASE_DIR, "config-tests.yml")
ms = MyMicroservice(path=os.path.join(os.path.dirname(os.path.dirname(__file__)), "project", "test_views.py"))
self.app = ms.create_app()
self.base_url = self.app.config["APPLICATION_ROOT"]

self.client = self.app.test_client()

def tearDown(self):
pass
ms.app = ms.create_app()
ms.base_url = ms.app.config["APPLICATION_ROOT"]
ms.client = ms.app.test_client()
return ms

def test_home(self):
response = self.client.get('/')
self.assertEqual(404, response.status_code)
def test_home(self, microservice):
response = microservice.client.get('/')
assert 404 == response.status_code

def test_healthcheck(self):
response = self.client.get('/healthcheck')
self.assertEqual(200, response.status_code)
def test_healthcheck(self, microservice):
response = microservice.client.get('/healthcheck')
assert 200 == response.status_code

def test_list_view(self):
response = self.client.get('/actors'.format(base_url=self.base_url))
self.assertEqual(200, response.status_code)
def test_list_view(self, microservice):
response = microservice.client.get('/actors'.format(base_url=microservice.base_url))
assert 200 == response.status_code

def test_pyms(self):
self.assertEqual("1234", self.app.config["TEST_VAR"])
def test_pyms(self, microservice):
assert "1234" == microservice.app.config["TEST_VAR"]

def test_create_view(self):
def test_create_view(self, microservice):
name = "Robert"
surname = "Downey Jr."
response = self.client.post('/actors'.format(
base_url=self.base_url),
response = microservice.client.post('/actors'.format(
base_url=microservice.base_url),
data=json.dumps(dict(name=name, surname=surname)),
content_type='application/json'
)
self.assertEqual(200, response.status_code)
self.assertEqual(name, _format_response(response.data)["name"])
assert 200 == response.status_code
assert name == _format_response(response.data)["name"]

0 comments on commit 51c3f6a

Please sign in to comment.