Fix suite_runner with logger. (#610)
diff --git a/mobly/suite_runner.py b/mobly/suite_runner.py
index d3ccce5..4da19e5 100644
--- a/mobly/suite_runner.py
+++ b/mobly/suite_runner.py
@@ -90,17 +90,18 @@
ok = True
for config in test_configs:
runner = test_runner.TestRunner(config.log_path, config.test_bed_name)
- for (test_class, tests) in selected_tests.items():
- runner.add_test_class(config, test_class, tests)
- try:
- runner.run()
- ok = runner.results.is_all_pass and ok
- except signals.TestAbortAll:
- pass
- except:
- logging.exception('Exception when executing %s.',
- config.test_bed_name)
- ok = False
+ with runner.mobly_logger():
+ for (test_class, tests) in selected_tests.items():
+ runner.add_test_class(config, test_class, tests)
+ try:
+ runner.run()
+ ok = runner.results.is_all_pass and ok
+ except signals.TestAbortAll:
+ pass
+ except:
+ logging.exception('Exception when executing %s.',
+ config.test_bed_name)
+ ok = False
if not ok:
sys.exit(1)
diff --git a/tests/mobly/controllers/android_device_lib/services/logcat_test.py b/tests/mobly/controllers/android_device_lib/services/logcat_test.py
index 1350a24..3106a02 100755
--- a/tests/mobly/controllers/android_device_lib/services/logcat_test.py
+++ b/tests/mobly/controllers/android_device_lib/services/logcat_test.py
@@ -66,7 +66,6 @@
class LogcatTest(unittest.TestCase):
"""Tests for Logcat service and its integration with AndroidDevice."""
-
def setUp(self):
# Set log_path to logging since mobly logger setup is not called.
if not hasattr(logging, 'log_path'):
@@ -504,7 +503,8 @@
@mock.patch('mobly.controllers.android_device_lib.adb.AdbProxy',
return_value=mock.MagicMock())
- @mock.patch('mobly.utils.stop_standing_subprocess')
+ @mock.patch('mobly.controllers.android_device_lib.fastboot.FastbootProxy',
+ return_value=mock_android_device.MockFastbootProxy('1'))
def test__enable_logpersist_with_missing_logpersist_start(
self, MockFastboot, MockAdbProxy):
def adb_shell_helper(command):
diff --git a/tests/mobly/suite_runner_test.py b/tests/mobly/suite_runner_test.py
index d0a9be4..57d6489 100755
--- a/tests/mobly/suite_runner_test.py
+++ b/tests/mobly/suite_runner_test.py
@@ -11,6 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
+
+import io
+import mock
+import os
+import shutil
+import tempfile
import unittest
from mobly import suite_runner
@@ -20,17 +26,23 @@
class SuiteRunnerTest(unittest.TestCase):
+ def setUp(self):
+ self.tmp_dir = tempfile.mkdtemp()
+
+ def tearDown(self):
+ shutil.rmtree(self.tmp_dir)
+
def test_select_no_args(self):
- identifiers = suite_runner.compute_selected_tests(
- test_classes=[
- integration_test.IntegrationTest,
- integration2_test.Integration2Test
- ],
- selected_tests=None)
- self.assertEqual({
- integration_test.IntegrationTest: None,
- integration2_test.Integration2Test: None,
- }, identifiers)
+ identifiers = suite_runner.compute_selected_tests(test_classes=[
+ integration_test.IntegrationTest,
+ integration2_test.Integration2Test
+ ],
+ selected_tests=None)
+ self.assertEqual(
+ {
+ integration_test.IntegrationTest: None,
+ integration2_test.Integration2Test: None,
+ }, identifiers)
def test_select_by_class(self):
identifiers = suite_runner.compute_selected_tests(
@@ -50,9 +62,9 @@
selected_tests=[
'IntegrationTest.test_a', 'IntegrationTest.test_b'
])
- self.assertEqual({
- integration_test.IntegrationTest: ['test_a', 'test_b']
- }, identifiers)
+ self.assertEqual(
+ {integration_test.IntegrationTest: ['test_a', 'test_b']},
+ identifiers)
def test_select_all_clobbers_method(self):
identifiers = suite_runner.compute_selected_tests(
@@ -71,6 +83,39 @@
selected_tests=['IntegrationTest', 'IntegrationTest.test_a'])
self.assertEqual({integration_test.IntegrationTest: None}, identifiers)
+ @mock.patch('sys.exit')
+ def test_run_suite(self, mock_exit):
+ tmp_file_path = os.path.join(self.tmp_dir, 'config.yml')
+ with io.open(tmp_file_path, 'w', encoding='utf-8') as f:
+ f.write(u"""
+ TestBeds:
+ # A test bed where adb will find Android devices.
+ - Name: SampleTestBed
+ Controllers:
+ MagicDevice: '*'
+ TestParams:
+ icecream: 42
+ extra_param: 'haha'
+ """)
+ suite_runner.run_suite([integration_test.IntegrationTest],
+ argv=['-c', tmp_file_path])
+ mock_exit.assert_not_called()
+
+ @mock.patch('sys.exit')
+ def test_run_suite_with_failures(self, mock_exit):
+ tmp_file_path = os.path.join(self.tmp_dir, 'config.yml')
+ with io.open(tmp_file_path, 'w', encoding='utf-8') as f:
+ f.write(u"""
+ TestBeds:
+ # A test bed where adb will find Android devices.
+ - Name: SampleTestBed
+ Controllers:
+ MagicDevice: '*'
+ """)
+ suite_runner.run_suite([integration_test.IntegrationTest],
+ argv=['-c', tmp_file_path])
+ mock_exit.assert_called_once_with(1)
+
if __name__ == "__main__":
unittest.main()