Skip to content

Commit 045f016

Browse files
authored
rework unit tests in the Pytest style. (#77)
1 parent 20b9e3c commit 045f016

File tree

5 files changed

+95
-93
lines changed

5 files changed

+95
-93
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ python:
1010
- "3.7"
1111
- "3.8"
1212
install:
13-
- pip install pycodestyle pydocstyle
13+
- pip install pycodestyle pydocstyle pytest delayed-assert requests
1414
before_script:
1515
- pycodestyle .
1616
- pydocstyle .
1717
script:
18-
- python setup.py test
18+
- pytest tests/ -s -vv
1919
- python setup.py -q install
2020
jobs:
2121
include:

setup.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,5 @@
2222
'Programming Language :: Python :: 3.7',
2323
'Programming Language :: Python :: 3.8'
2424
],
25-
install_requires=['requests>=2.4.2', 'six'],
26-
setup_requires=['pytest-runner'],
27-
tests_require=[
28-
'pytest',
29-
'delayed-assert'
30-
]
25+
install_requires=['requests>=2.4.2', 'six']
3126
)

tests/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
"""tests for client Python."""
1+
"""This package contains unit tests for the project."""
2+
3+
from six import add_move, MovedModule
4+
add_move(MovedModule('mock', 'mock', 'unittest.mock'))

tests/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""This module contains common Pytest fixtures and hooks for unit tests."""
2+
3+
from six.moves import mock
4+
5+
from pytest import fixture
6+
7+
from reportportal_client.service import ReportPortalService
8+
9+
10+
@fixture()
11+
def response():
12+
"""Cook up a mock for the Response with specific arguments."""
13+
def inner(ret_code, ret_value):
14+
"""Set up response with the given parameters.
15+
16+
:param ret_code: Return code for the response
17+
:param ret_value: Return value for the response
18+
:return: Mocked Responce object with the given parameters
19+
"""
20+
with mock.patch('requests.Response') as resp:
21+
resp.status_code = ret_code
22+
resp.json.return_value = ret_value
23+
return resp
24+
return inner
25+
26+
27+
@fixture(scope='session')
28+
def rp_service():
29+
"""Prepare instance of the ReportPortalService for testing."""
30+
service = ReportPortalService('http://endpoint', 'project', 'token')
31+
service.session = mock.Mock()
32+
return service

tests/test_service.py

Lines changed: 56 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,84 @@
1-
"""Tests for service.py ."""
1+
"""This modules includes unit tests for the service.py module."""
22

3-
try:
4-
from unittest.mock import create_autospec, Mock, patch, MagicMock
5-
except ImportError:
6-
from mock import create_autospec, Mock, patch, MagicMock
73

8-
from reportportal_client.service import _convert_string, _list_to_payload, \
9-
uri_join, _get_id, _get_msg, _get_data, _get_json, _get_messages, \
10-
ReportPortalService
11-
12-
import unittest
134
from datetime import datetime
5+
from six.moves import mock
6+
7+
from delayed_assert import expect, assert_expectations
8+
9+
from reportportal_client.service import (
10+
_convert_string,
11+
_get_data,
12+
_get_id,
13+
_get_json,
14+
_get_messages,
15+
_get_msg,
16+
_list_to_payload
17+
)
1418

1519

16-
class TestServiceFunctions(unittest.TestCase):
17-
"""Test for additional functions."""
20+
class TestServiceFunctions:
21+
"""This class contains test methods for helper functions."""
1822

1923
def test_check_convert_to_string(self):
2024
"""Test for support and convert strings to utf-8."""
21-
self.assertEqual(_convert_string("Hello world"), 'Hello world')
22-
self.assertEqual(type(_convert_string("Hello world")), str)
25+
expect(_convert_string("Hello world") == 'Hello world')
26+
expect(lambda: isinstance(_convert_string("Hello world"), str))
27+
assert_expectations()
2328

2429
def test_list_to_payload(self):
2530
"""Test convert dict to list of dicts."""
2631
initial_dict = {'key': "value", 'key1': 'value1'}
2732
expected_list = [{'key': 'key', 'value': 'value'},
2833
{'key': 'key1', 'value': 'value1'}]
29-
self.assertEqual(_list_to_payload(initial_dict), expected_list)
30-
31-
def test_get_id(self):
32-
"""Test for get id from Response obj."""
33-
fake_json = {"id": 123}
34+
assert _list_to_payload(initial_dict) == expected_list
3435

35-
with patch('requests.Response', new_callable=MagicMock()) as mock_get:
36-
mock_get.status_code = 200
37-
mock_get.json.return_value = fake_json
36+
def test_get_id(self, response):
37+
"""Test for the get_id function."""
38+
assert _get_id(response(200, {"id": 123})) == 123
3839

39-
obj = _get_id(mock_get)
40-
41-
self.assertEqual(obj, 123)
42-
43-
def test_get_msg(self):
44-
"""Test get_msg recieved from Response."""
40+
def test_get_msg(self, response):
41+
"""Test for the get_msg function."""
4542
fake_json = {"id": 123}
43+
assert _get_msg(response(200, fake_json)) == fake_json
4644

47-
with patch('requests.Response', new_callable=MagicMock()) as mock_get:
48-
mock_get.status_code = 200
49-
mock_get.json.return_value = fake_json
50-
51-
obj = _get_msg(mock_get)
52-
53-
self.assertEqual(obj, fake_json)
54-
55-
def test_get_data(self):
56-
"""Test get data from Response."""
45+
def test_get_data(self, response):
46+
"""Test for the get_data function."""
5747
fake_json = {"id": 123}
48+
assert _get_data(response(200, fake_json)) == fake_json
5849

59-
with patch('requests.Response', new_callable=MagicMock()) as mock_get:
60-
mock_get.status_code = 200
61-
mock_get.json.return_value = fake_json
62-
63-
obj = _get_data(mock_get)
64-
65-
self.assertEqual(obj, fake_json)
66-
67-
def test_get_json(self):
68-
"""Test get json from Response."""
50+
def test_get_json(self, response):
51+
"""Test for the get_json function."""
6952
fake_json = {"id": 123}
70-
71-
with patch('requests.Response', new_callable=MagicMock()) as mock_get:
72-
mock_get.status_code = 200
73-
mock_get.json.return_value = fake_json
74-
75-
obj = _get_json(mock_get)
76-
77-
self.assertEqual(obj, fake_json)
53+
assert _get_json(response(200, fake_json)) == fake_json
7854

7955
def test_get_messages(self):
80-
"""Test get errors from response."""
56+
"""Test for the get_messages function."""
8157
data = {"responses": [{"errorCode": 422, "message": "error"}]}
82-
83-
obj = _get_messages(data)
84-
85-
self.assertEqual(obj, ['422: error'])
58+
assert _get_messages(data) == ['422: error']
8659

8760

88-
class ReportPortalServiceTest(unittest.TestCase):
61+
class TestReportPortalService:
8962
"""This class stores methods which test ReportPortalService."""
9063

91-
def setUp(self):
92-
"""Instantiate the ReportPortalService class and mock its session."""
93-
self.rp = ReportPortalService('http://endpoint', 'project', 'token')
94-
self.rp.session = MagicMock()
95-
96-
def test_start_launch(self):
97-
"""Test start launch and sending request."""
98-
with patch('reportportal_client.service._get_data',
99-
new_callable=Mock()) as mock_get:
100-
mock_get.return_value = {"id": 111}
101-
launch_id = self.rp.start_launch('name',
102-
datetime.now().isoformat())
103-
self.assertEqual(launch_id, 111)
104-
105-
def test_finish_launch(self):
106-
"""Test finish launch and sending request."""
107-
with patch('reportportal_client.service._get_msg',
108-
new_callable=Mock()) as mock_get:
109-
mock_get.return_value = {"id": 111}
110-
_get_msg = self.rp.finish_launch('name',
111-
datetime.now().isoformat())
112-
self.assertEqual(_get_msg, {"id": 111})
64+
@mock.patch('reportportal_client.service._get_data')
65+
def test_start_launch(self, mock_get, rp_service):
66+
"""Test start launch and sending request.
67+
68+
:param mock_get: Mocked _get_data() function
69+
:param rp_service: Pytest fixture
70+
"""
71+
mock_get.return_value = {"id": 111}
72+
launch_id = rp_service.start_launch('name', datetime.now().isoformat())
73+
assert launch_id == 111
74+
75+
@mock.patch('reportportal_client.service._get_msg')
76+
def test_finish_launch(self, mock_get, rp_service):
77+
"""Test finish launch and sending request.
78+
79+
:param mock_get: Mocked _get_msg() function
80+
:param rp_service: Pytest fixture
81+
"""
82+
mock_get.return_value = {"id": 111}
83+
_get_msg = rp_service.finish_launch('name', datetime.now().isoformat())
84+
assert _get_msg == {"id": 111}

0 commit comments

Comments
 (0)