diff --git a/release.sh b/release.sh index cf812f8..736385b 100755 --- a/release.sh +++ b/release.sh @@ -93,8 +93,55 @@ get_android_versions get_processors function test() { - (export ANDROID_HOME=/root && export ANDROID_VERSION=5.0.1 && export API_LEVEL=21 \ - && export PROCESSOR=x86 && export SYS_IMG=x86_64 && export IMG_TYPE=google_apis && nosetests -v) + # Prepare needed parameter to run tests + test_android_version=7.1.1 + test_api_level=25 + test_processor=x86 + test_sys_img=x86_64 + test_img_type=google_apis + test_image=test_img + test_container=test_con + + # Run unit test + echo "----UNIT TEST----" + (export ANDROID_HOME=/root && export ANDROID_VERSION=$test_android_version && export API_LEVEL=$test_api_level \ + && export PROCESSOR=$test_processor && export SYS_IMG=$test_sys_img && export IMG_TYPE=$test_img_type \ + && nosetests src/tests/unit -v) + + # Run integration test + # Integration test must be run only for linux OS / x86 image to reduce duration of test execution + if [ "$(uname -s)" == 'Linux' ]; then + echo "----BUILD TEST IMAGE----" + docker build -t $test_image --build-arg ANDROID_VERSION=$test_android_version \ + --build-arg BUILD_TOOL=$LATEST_BUILD_TOOL --build-arg API_LEVEL=$test_api_level \ + --build-arg PROCESSOR=$test_processor --build-arg SYS_IMG=$test_sys_img \ + --build-arg IMG_TYPE=$test_img_type -f docker/Emulator_x86 . + + echo "----RUN E2E TEST----" + docker run --privileged -d -p 4723:4723 -p 6080:6080 -e APPIUM=True --name $test_container $test_image + attempt=0 + while [ ${attempt} -le 10 ]; do + attempt=$(($attempt + 1)) + output=$(docker ps | grep healthy | grep test_con | wc -l) + if [[ "$output" == 1 ]]; then + echo "Emulator is ready." + break + else + echo "Waiting 10 seconds for emulator to be ready (attempt: $attempt)" + sleep 10 + fi + + if [[ $attempt == 10 ]]; then + echo "Failed!" + exit 1 + fi + done + + nosetests src/tests/e2e -v + + echo "----REMOVE TEST CONTAINER----" + docker kill $test_container && docker rm $test_container + fi } function build() { diff --git a/requirements.txt b/requirements.txt index 95aaf43..2d2183c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +Appium-Python-Client==0.24 coverage==4.2 mock==2.0.0 nose==1.3.7 diff --git a/src/tests/__init__.py b/src/tests/__init__.py index 93c555e..e69de29 100644 --- a/src/tests/__init__.py +++ b/src/tests/__init__.py @@ -1,54 +0,0 @@ -"""Unit test to test app.""" -import os -from unittest import TestCase - -import mock - -from src import app - - -class TestApp(TestCase): - """Unit test class to test other methods in the app.""" - - def test_valid_env(self): - key = 'ENV_1' - os.environ[key] = 'test' - app.get_or_raise(key) - del os.environ[key] - - def test_invalid_env(self): - with self.assertRaises(RuntimeError): - app.get_or_raise('ENV_2') - - def test_valid_bool(self): - self.assertEqual(app.convert_str_to_bool('True'), True) - self.assertEqual(app.convert_str_to_bool('t'), True) - self.assertEqual(app.convert_str_to_bool('1'), True) - self.assertEqual(app.convert_str_to_bool('YES'), True) - - def test_invalid_bool(self): - self.assertEqual(app.convert_str_to_bool(''), False) - self.assertEqual(app.convert_str_to_bool('test'), False) - - def test_invalid_format(self): - self.assertEqual(app.convert_str_to_bool(True), None) - - @mock.patch('src.app.prepare_avd') - @mock.patch('subprocess.Popen') - def test_run_with_appium(self, mocked_avd, mocked_subprocess): - with mock.patch('src.app.appium_run') as mocked_appium: - os.environ['APPIUM'] = str(True) - app.run() - self.assertTrue(mocked_avd.called) - self.assertTrue(mocked_subprocess.called) - self.assertTrue(mocked_appium.called) - - @mock.patch('src.app.prepare_avd') - @mock.patch('subprocess.Popen') - def test_run_withhout_appium(self, mocked_avd, mocked_subprocess): - with mock.patch('src.app.appium_run') as mocked_appium: - os.environ['APPIUM'] = str(False) - app.run() - self.assertTrue(mocked_avd.called) - self.assertTrue(mocked_subprocess.called) - self.assertFalse(mocked_appium.called) diff --git a/src/tests/e2e/__init__.py b/src/tests/e2e/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/e2e/test_chrome.py b/src/tests/e2e/test_chrome.py new file mode 100644 index 0000000..746e334 --- /dev/null +++ b/src/tests/e2e/test_chrome.py @@ -0,0 +1,22 @@ +"""e2e test to test chrome application inside docker-android""" +from unittest import TestCase + +from appium import webdriver + + +class TestE2EChrome(TestCase): + + def setUp(self): + desired_caps = { + 'platformName': 'Android', + 'deviceName': 'Android Emulator', + 'appPackage': 'com.android.chrome', + 'appActivity': 'com.google.android.apps.chrome.Main' + } + self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) + + def test_open_url(self): + self.driver.get('http://google.com') + + def tearDown(self): + self.driver.quit() diff --git a/src/tests/unit/__init__.py b/src/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/unit/test_app.py b/src/tests/unit/test_app.py new file mode 100644 index 0000000..93c555e --- /dev/null +++ b/src/tests/unit/test_app.py @@ -0,0 +1,54 @@ +"""Unit test to test app.""" +import os +from unittest import TestCase + +import mock + +from src import app + + +class TestApp(TestCase): + """Unit test class to test other methods in the app.""" + + def test_valid_env(self): + key = 'ENV_1' + os.environ[key] = 'test' + app.get_or_raise(key) + del os.environ[key] + + def test_invalid_env(self): + with self.assertRaises(RuntimeError): + app.get_or_raise('ENV_2') + + def test_valid_bool(self): + self.assertEqual(app.convert_str_to_bool('True'), True) + self.assertEqual(app.convert_str_to_bool('t'), True) + self.assertEqual(app.convert_str_to_bool('1'), True) + self.assertEqual(app.convert_str_to_bool('YES'), True) + + def test_invalid_bool(self): + self.assertEqual(app.convert_str_to_bool(''), False) + self.assertEqual(app.convert_str_to_bool('test'), False) + + def test_invalid_format(self): + self.assertEqual(app.convert_str_to_bool(True), None) + + @mock.patch('src.app.prepare_avd') + @mock.patch('subprocess.Popen') + def test_run_with_appium(self, mocked_avd, mocked_subprocess): + with mock.patch('src.app.appium_run') as mocked_appium: + os.environ['APPIUM'] = str(True) + app.run() + self.assertTrue(mocked_avd.called) + self.assertTrue(mocked_subprocess.called) + self.assertTrue(mocked_appium.called) + + @mock.patch('src.app.prepare_avd') + @mock.patch('subprocess.Popen') + def test_run_withhout_appium(self, mocked_avd, mocked_subprocess): + with mock.patch('src.app.appium_run') as mocked_appium: + os.environ['APPIUM'] = str(False) + app.run() + self.assertTrue(mocked_avd.called) + self.assertTrue(mocked_subprocess.called) + self.assertFalse(mocked_appium.called) diff --git a/src/tests/test_appium.py b/src/tests/unit/test_appium.py similarity index 100% rename from src/tests/test_appium.py rename to src/tests/unit/test_appium.py diff --git a/src/tests/test_avd.py b/src/tests/unit/test_avd.py similarity index 100% rename from src/tests/test_avd.py rename to src/tests/unit/test_avd.py