diff --git a/tests/cli_test_case.py b/tests/cli_test_case.py index 042f601ed..8aa154c92 100644 --- a/tests/cli_test_case.py +++ b/tests/cli_test_case.py @@ -3,12 +3,14 @@ import os import unittest import types +import responses import click.testing from click.testing import CliRunner from launchable.__main__ import main from launchable.utils.session import clean_session_files +from launchable.utils.http_client import get_base_url class CliTestCase(unittest.TestCase): @@ -28,6 +30,14 @@ def setUp(self): self.maxDiff = None os.environ['LAUNCHABLE_TOKEN'] = self.launchable_token + responses.add(responses.POST, "{}/intake/organizations/{}/workspaces/{}/builds/{}/test_sessions".format(get_base_url(), self.organization, self.workspace, self.build_name), + json={'id': self.session_id}, status=200) + responses.add(responses.POST, "{}/intake/organizations/{}/workspaces/{}/subset".format(get_base_url(), self.organization, self.workspace), + json={'testPaths': []}, status=200) + + responses.add(responses.POST, "{}/intake/organizations/{}/workspaces/{}/subset".format(get_base_url(), self.organization, self.workspace,), + json={'testPaths': []}, status=200) + def tearDown(self): clean_session_files() diff --git a/tests/commands/test_record_tests_go_test.py b/tests/test_runners/test_go_test.py similarity index 68% rename from tests/commands/test_record_tests_go_test.py rename to tests/test_runners/test_go_test.py index 377618479..324028d68 100644 --- a/tests/commands/test_record_tests_go_test.py +++ b/tests/test_runners/test_go_test.py @@ -1,9 +1,7 @@ from pathlib import Path -from unittest import mock import responses import json import gzip -from launchable.utils.http_client import get_base_url from launchable.utils.session import read_session from tests.cli_test_case import CliTestCase @@ -12,25 +10,21 @@ class GoTestTest(CliTestCase): test_files_dir = Path(__file__).parent.joinpath( '../data/go_test/').resolve() - @mock.patch('requests.request') - def test_subset_with_session(self, mock_post): + @responses.activate + def test_subset_with_session(self): pipe = "TestExample1\nTestExample2\nTestExample3\nTestExample4\nok github.com/launchableinc/rocket-car-gotest 0.268s" result = self.cli('subset', '--target', '10%', '--session', self.session, 'go-test', input=pipe) self.assertEqual(result.exit_code, 0) - payload = self.gzipped_json_payload(mock_post) + payload = json.loads(gzip.decompress( + responses.calls[0].request.body).decode()) expected = self.load_json_from_file( self.test_files_dir.joinpath('subset_result.json')) self.assert_json_orderless_equal(expected, payload) @responses.activate def test_subset_without_session(self): - responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/builds/{}/test_sessions".format(get_base_url(), self.build_name), - json={'id': self.session_id}, status=200) - responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/subset".format(get_base_url()), - json={'testPaths': []}, status=200) - pipe = "TestExample1\nTestExample2\nTestExample3\nTestExample4\nok github.com/launchableinc/rocket-car-gotest 0.268s" result = self.cli('subset', '--target', '10%', '--build', self.build_name, 'go-test', input=pipe) @@ -44,13 +38,14 @@ def test_subset_without_session(self): self.test_files_dir.joinpath('subset_result.json')) self.assert_json_orderless_equal(expected, payload) - @mock.patch('requests.request') - def test_record_tests_with_session(self, mock_post): + @responses.activate + def test_record_tests_with_session(self): result = self.cli('record', 'tests', '--session', self.session, 'go-test', str(self.test_files_dir) + "/") self.assertEqual(result.exit_code, 0) - payload = self.gzipped_json_payload(mock_post) + payload = json.loads(gzip.decompress( + b''.join(responses.calls[0].request.body)).decode()) # Remove timestamp because it depends on the machine clock for c in payload['events']: del c['created_at'] @@ -61,11 +56,6 @@ def test_record_tests_with_session(self, mock_post): @responses.activate def test_record_tests_without_session(self): - responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/builds/{}/test_sessions".format(get_base_url(), self.build_name), - json={'id': self.session_id}, status=200) - responses.add(responses.POST, "{}/intake/organizations/launchableinc/workspaces/mothership/builds/{}/test_sessions/{}/events".format(get_base_url(), self.build_name, self.session_id), - json={}, status=200) - result = self.cli('record', 'tests', '--build', self.build_name, 'go-test', str(self.test_files_dir) + "/") self.assertEqual(result.exit_code, 0) diff --git a/tests/test_runners/test_googletest.py b/tests/test_runners/test_googletest.py index c957f5017..84dee26f1 100644 --- a/tests/test_runners/test_googletest.py +++ b/tests/test_runners/test_googletest.py @@ -1,6 +1,7 @@ from pathlib import Path -from unittest import mock - +import responses +import json +import gzip from tests.cli_test_case import CliTestCase @@ -8,26 +9,28 @@ class GoogleTestTest(CliTestCase): test_files_dir = Path(__file__).parent.joinpath( '../data/googletest/').resolve() - @mock.patch('requests.request') - def test_subset(self, mock_post): + @responses.activate + def test_subset(self): # I use "ctest -N" to get this list. pipe = "Test project github.com/launchableinc/rocket-car-googletest\n Test #1: FooTest.Bar\n Test #2: FooTest.Baz\n Test #3: FooTest.Foo\n Test #4: */ParameterizedTest.Bar/*\n\nTotal Tests: 4" result = self.cli('subset', '--target', '10%', '--session', self.session, 'googletest', input=pipe) self.assertEqual(result.exit_code, 0) - payload = self.gzipped_json_payload(mock_post) + payload = json.loads(gzip.decompress( + responses.calls[0].request.body).decode()) expected = self.load_json_from_file( self.test_files_dir.joinpath('subset_result.json')) self.assert_json_orderless_equal(expected, payload) - @mock.patch('requests.request') - def test_record_test_googletest(self, mock_post): + @responses.activate + def test_record_test_googletest(self): result = self.cli('record', 'tests', '--session', self.session, 'googletest', str(self.test_files_dir) + "/") self.assertEqual(result.exit_code, 0) - payload = self.gzipped_json_payload(mock_post) + payload = json.loads(gzip.decompress( + b''.join(responses.calls[0].request.body)).decode()) expected = self.load_json_from_file( self.test_files_dir.joinpath('record_test_result.json')) diff --git a/tests/test_runners/test_gradle.py b/tests/test_runners/test_gradle.py index edc071c2f..6f20879b0 100644 --- a/tests/test_runners/test_gradle.py +++ b/tests/test_runners/test_gradle.py @@ -1,6 +1,8 @@ from pathlib import Path from unittest import mock - +import responses +import json +import gzip from tests.cli_test_case import CliTestCase @@ -8,12 +10,13 @@ class GradleTest(CliTestCase): test_files_dir = Path(__file__).parent.joinpath('../data/gradle/recursion').resolve() result_file_path = test_files_dir.joinpath('expected.json') - @mock.patch('requests.request') - def test_record_test_gradle(self, mock_post): + @responses.activate + def test_record_test_gradle(self): result = self.cli('record', 'tests', '--session', self.session, 'gradle', str(self.test_files_dir) + "/**/reports") self.assertEqual(result.exit_code, 0) - payload = self.gzipped_json_payload(mock_post) - expected = self.load_json_from_file(self.result_file_path) + payload = json.loads(gzip.decompress( + b''.join(responses.calls[0].request.body)).decode()) + expected = self.load_json_from_file(self.result_file_path) self.assert_json_orderless_equal(expected, payload) diff --git a/tests/commands/test_record_tests_minitest.py b/tests/test_runners/test_minitest.py similarity index 71% rename from tests/commands/test_record_tests_minitest.py rename to tests/test_runners/test_minitest.py index d28817ce0..238dd611e 100644 --- a/tests/commands/test_record_tests_minitest.py +++ b/tests/test_runners/test_minitest.py @@ -1,6 +1,7 @@ from pathlib import Path -from unittest import mock - +import responses +import json +import gzip from tests.cli_test_case import CliTestCase @@ -8,11 +9,13 @@ class MinitestTest(CliTestCase): test_files_dir = Path(__file__).parent.joinpath('../data/minitest/').resolve() result_file_path = test_files_dir.joinpath('record_test_result.json') - @mock.patch('requests.request') - def test_record_test_minitest(self, mock_post): + @responses.activate + def test_record_test_minitest(self): result = self.cli('record', 'tests', '--session', self.session, 'minitest', str(self.test_files_dir) + "/") self.assertEqual(result.exit_code, 0) - payload = self.gzipped_json_payload(mock_post) + payload = json.loads(gzip.decompress( + b''.join(responses.calls[0].request.body)).decode()) + expected = self.load_json_from_file(self.result_file_path) self.assert_json_orderless_equal(expected, payload)