1#!/usr/bin/env python 2# 3# Copyright 2018, The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16"""Unittests for robolectric_test_runner.""" 17 18import json 19import unittest 20import subprocess 21import tempfile 22import mock 23 24import event_handler 25# pylint: disable=import-error 26from test_finders import test_info 27from test_runners import robolectric_test_runner 28 29# pylint: disable=protected-access 30class RobolectricTestRunnerUnittests(unittest.TestCase): 31 """Unit tests for robolectric_test_runner.py""" 32 33 def setUp(self): 34 self.polling_time = robolectric_test_runner.POLL_FREQ_SECS 35 self.suite_tr = robolectric_test_runner.RobolectricTestRunner(results_dir='') 36 37 def tearDown(self): 38 mock.patch.stopall() 39 40 @mock.patch.object(robolectric_test_runner.RobolectricTestRunner, 'run') 41 def test_run_tests_raw(self, mock_run): 42 """Test run_tests_raw method.""" 43 test_infos = [test_info.TestInfo("Robo1", 44 "RobolectricTestRunner", 45 ["RoboTest"])] 46 extra_args = [] 47 mock_subproc = mock.Mock() 48 mock_run.return_value = mock_subproc 49 mock_subproc.returncode = 0 50 mock_reporter = mock.Mock() 51 # Test Build Pass 52 self.assertEqual( 53 0, 54 self.suite_tr.run_tests_raw(test_infos, extra_args, mock_reporter)) 55 # Test Build Fail 56 mock_subproc.returncode = 1 57 self.assertNotEqual( 58 0, 59 self.suite_tr.run_tests_raw(test_infos, extra_args, mock_reporter)) 60 61 @mock.patch.object(event_handler.EventHandler, 'process_event') 62 def test_exec_with_robo_polling_complete_information(self, mock_pe): 63 """Test _exec_with_robo_polling method.""" 64 event_name = 'TEST_STARTED' 65 event_data = {'className':'SomeClass', 'testName':'SomeTestName'} 66 67 json_event_data = json.dumps(event_data) 68 data = '%s %s\n\n' %(event_name, json_event_data) 69 event_file = tempfile.NamedTemporaryFile(mode='w+r', delete=True) 70 subprocess.call("echo '%s' -n >> %s" %(data, event_file.name), shell=True) 71 robo_proc = subprocess.Popen("sleep %s" %str(self.polling_time * 2), shell=True) 72 self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe) 73 calls = [mock.call.process_event(event_name, event_data)] 74 mock_pe.assert_has_calls(calls) 75 76 @mock.patch.object(event_handler.EventHandler, 'process_event') 77 def test_exec_with_robo_polling_with_partial_info(self, mock_pe): 78 """Test _exec_with_robo_polling method.""" 79 event_name = 'TEST_STARTED' 80 event1 = '{"className":"SomeClass","test' 81 event2 = 'Name":"SomeTestName"}\n\n' 82 data1 = '%s %s'%(event_name, event1) 83 data2 = event2 84 event_file = tempfile.NamedTemporaryFile(mode='w+r', delete=True) 85 subprocess.Popen("echo -n '%s' >> %s" %(data1, event_file.name), shell=True) 86 robo_proc = subprocess.Popen("echo '%s' >> %s && sleep %s" 87 %(data2, 88 event_file.name, 89 str(self.polling_time*5)), 90 shell=True) 91 self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe) 92 calls = [mock.call.process_event(event_name, 93 json.loads(event1 + event2))] 94 mock_pe.assert_has_calls(calls) 95 96 @mock.patch.object(event_handler.EventHandler, 'process_event') 97 def test_exec_with_robo_polling_with_fail_stacktrace(self, mock_pe): 98 """Test _exec_with_robo_polling method.""" 99 event_name = 'TEST_FAILED' 100 event_data = {'className':'SomeClass', 'testName':'SomeTestName', 101 'trace':'{"trace":"AssertionError: <true> is equal to <false>\n' 102 'at FailureStrategy.fail(FailureStrategy.java:24)\n' 103 'at FailureStrategy.fail(FailureStrategy.java:20)\n'} 104 data = '%s %s\n\n'%(event_name, json.dumps(event_data)) 105 event_file = tempfile.NamedTemporaryFile(mode='w+r', delete=True) 106 subprocess.call("echo '%s' -n >> %s" %(data, event_file.name), shell=True) 107 robo_proc = subprocess.Popen("sleep %s" %str(self.polling_time * 2), shell=True) 108 self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe) 109 calls = [mock.call.process_event(event_name, event_data)] 110 mock_pe.assert_has_calls(calls) 111 112 @mock.patch.object(event_handler.EventHandler, 'process_event') 113 def test_exec_with_robo_polling_with_multi_event(self, mock_pe): 114 """Test _exec_with_robo_polling method.""" 115 event_file = tempfile.NamedTemporaryFile(mode='w+r', delete=True) 116 events = [ 117 ('TEST_MODULE_STARTED', { 118 'moduleContextFileName':'serial-util1146216{974}2772610436.ser', 119 'moduleName':'someTestModule'}), 120 ('TEST_RUN_STARTED', {'testCount': 2}), 121 ('TEST_STARTED', {'start_time':52, 'className':'someClassName', 122 'testName':'someTestName'}), 123 ('TEST_ENDED', {'end_time':1048, 'className':'someClassName', 124 'testName':'someTestName'}), 125 ('TEST_STARTED', {'start_time':48, 'className':'someClassName2', 126 'testName':'someTestName2'}), 127 ('TEST_FAILED', {'className':'someClassName2', 'testName':'someTestName2', 128 'trace': 'someTrace'}), 129 ('TEST_ENDED', {'end_time':9876450, 'className':'someClassName2', 130 'testName':'someTestName2'}), 131 ('TEST_RUN_ENDED', {}), 132 ('TEST_MODULE_ENDED', {'foo': 'bar'}),] 133 data = '' 134 for event in events: 135 data += '%s %s\n\n'%(event[0], json.dumps(event[1])) 136 137 subprocess.call("echo '%s' -n >> %s" %(data, event_file.name), shell=True) 138 robo_proc = subprocess.Popen("sleep %s" %str(self.polling_time * 2), shell=True) 139 self.suite_tr. _exec_with_robo_polling(event_file, robo_proc, mock_pe) 140 calls = [mock.call.process_event(name, data) for name, data in events] 141 mock_pe.assert_has_calls(calls) 142 143if __name__ == '__main__': 144 unittest.main() 145