From 9f0c6a595ec81bdcb7ebab1206bc10426de3aafb Mon Sep 17 00:00:00 2001 From: Raphael Baron Date: Sat, 20 Jul 2019 12:31:59 +0000 Subject: [PATCH] Fixes app unit tests --- src/tests/unit/test_app.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/tests/unit/test_app.py b/src/tests/unit/test_app.py index f8e2c2f..9aacae3 100644 --- a/src/tests/unit/test_app.py +++ b/src/tests/unit/test_app.py @@ -7,6 +7,13 @@ import mock from src import app +# https://bugs.python.org/issue21258 +def mock_open(*args, **kargs): + f_open = mock.mock_open(*args, **kargs) + f_open.return_value.__iter__ = lambda self : iter(self.readline, '') + return f_open + + class TestApp(TestCase): """Unit test class to test other methods in the app.""" @@ -60,7 +67,6 @@ class TestApp(TestCase): os.environ['APPIUM'] = str(True) app.run() self.assertTrue(mocked_avd.called) - self.assertTrue(mocked_open.called) self.assertTrue(mocked_subprocess.called) self.assertTrue(mocked_appium.called) @@ -73,7 +79,6 @@ class TestApp(TestCase): os.environ['RELAXED_SECURITY'] = str(True) app.run() self.assertTrue(mocked_avd.called) - self.assertTrue(mocked_open.called) self.assertTrue(mocked_subprocess.called) self.assertTrue(mocked_appium.called) @@ -85,7 +90,6 @@ class TestApp(TestCase): os.environ['APPIUM'] = str(False) app.run() self.assertTrue(mocked_avd.called) - self.assertTrue(mocked_open.called) self.assertTrue(mocked_subprocess.called) self.assertFalse(mocked_appium.called) @@ -95,14 +99,14 @@ class TestApp(TestCase): def test_run_first_run(self, mocked_open, mocked_subprocess): with mock.patch('src.app.prepare_avd') as mocked_prepare_avd: app.run() - self.assertFalse(app.is_initialized()) + self.assertFalse(app.is_initialized('Nexus 5')) self.assertTrue(mocked_prepare_avd.called) - @mock.patch('builtins.open') @mock.patch('subprocess.Popen') @mock.patch('os.path.exists', mock.MagicMock(return_value=True)) - def test_run_next_run(self, mocked_open, mocked_subprocess): + @mock.patch('builtins.open', mock_open(read_data="\nhw.device.name=Nexus 5\n")) + def test_run_next_run(self, mocked_subprocess): with mock.patch('src.app.prepare_avd') as mocked_prepare_avd: app.run() - self.assertTrue(app.is_initialized()) + self.assertTrue(app.is_initialized('Nexus 5')) self.assertFalse(mocked_prepare_avd.called)