Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android unit tests analytics #17

Closed
wants to merge 12 commits into from
111 changes: 111 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
version: 2.1

executors:
docker-executor:
docker:
- image: circleci/android:api-29
working_directory: ~/amplify-flutter

macos-executor:
macos:
xcode: 11.6.0
working_directory: ~/amplify-flutter

commands:
install_flutter:
description:
Install Flutter and set up paths.
parameters:
flutter_branch:
description: Flutter branch or version tag.
type: string
default: stable
steps:
- run:
name: Set up Flutter
command: |
echo 'export FLUTTER_HOME=${HOME}/sdks/flutter' >> $BASH_ENV
echo 'export FLUTTER_BRANCH=<< parameters.flutter_branch >>' >> $BASH_ENV
echo 'export FLUTTER_ROOT=${FLUTTER_HOME}' >> $BASH_ENV
echo 'export PATH=${PATH}:${FLUTTER_HOME}/bin:${FLUTTER_HOME}/bin/cache/dart-sdk/bin:${HOME}/.pub-cache/bin:${FLUTTER_HOME}/.pub-cache/bin' >> $BASH_ENV
source $BASH_ENV
git clone --branch ${FLUTTER_BRANCH} https://github.com/flutter/flutter.git ${FLUTTER_HOME}
(yes || true) | flutter doctor --android-licenses && flutter doctor

jobs:
unit_test_flutter:
executor: docker-executor
steps:
- install_flutter
- checkout
- run:
name: Install junitreport for JUnit XML reports
command: flutter pub global activate junitreport
- run:
name: Run Flutter Unit Tests
command: . .circleci/test_all_plugins.sh flutter-test
- run:
name: Save test results
command: |
mkdir -p ~/test-results/junit/
find . -type f -regex "\./packages/.*/test-results/.*-flutter-test\.xml" -exec cp {} ~/test-results/junit/ \;
when: always
- store_test_results:
path: ~/test-results
- store_artifacts:
path: ~/test-results/junit

unit_test_ios:
executor: macos-executor
steps:
- install_flutter:
flutter_branch: 1.17.5
- checkout
- run:
name: Pre-start iOS simulator
# xcrun instruments returns non zero, but successfully starts the simulator
command: xcrun instruments -w "iPhone 11 (13.6) [" || true
- run:
name: Run iOS Unit Tests
command: . .circleci/test_all_plugins.sh ios-test
- run:
name: Save test results
command: |
mkdir -p ~/test-results/junit/
find . -type f -regex "\./packages/.*/example/ios/test-results/.*-xcodebuild-test\.xml" -exec cp {} ~/test-results/junit/ \;
when: always
- store_test_results:
path: ~/test-results
- store_artifacts:
path: ~/test-results/junit

unit_test_android:
executor: docker-executor
steps:
- install_flutter
- checkout
- run:
name: Run Android Unit Tests
command: . .circleci/test_all_plugins.sh android-test
- run:
name: Save test results
command: |
mkdir -p ~/test-results/junit/
find . -type f -regex "\./packages/.*/example/build/.*/test-results/.*\.xml" -exec cp {} ~/test-results/junit/ \;
when: always
- store_test_results:
path: ~/test-results
- store_artifacts:
path: ~/test-results/junit

releasable_branches: &releasable_branches
branches:
only:
- release
- master

workflows:
test_deploy:
jobs:
- unit_test_flutter
- unit_test_android
117 changes: 117 additions & 0 deletions .circleci/test_all_plugins.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/bin/bash

cd packages || exit

passed_plugins=()
failed_plugins=()
skipped_plugins=()

set +e

for plugin_dir in */; do
cd "$plugin_dir" || exit
plugin=$(basename "$plugin_dir")
case $1 in
flutter-test)
echo "=== Running Flutter unit tests for $plugin ==="
if [ -d "test" ]; then
mkdir -p test-results
if flutter test --machine | tojunit --output "test-results/$plugin-flutter-test.xml"; then
echo "PASSED: Flutter unit tests for $plugin passed."
passed_plugins+=("$plugin")
else
echo "FAILED: Flutter unit tests for $plugin failed."
failed_plugins+=("$plugin")
fi
else
echo "SKIPPED: Flutter unit tests for $plugin don't exist. Skipping."
skipped_plugins+=("$plugin")
fi
;;
android-test)
echo "=== Running Android unit tests for $plugin ==="
if [ -d "android/src/test" ]; then
if [ ! -d "example/android" ]; then
echo "FAILED: example/android missing, can't run tests."
failed_plugins+=("$plugin")
continue
fi
cd example/android
if ! flutter build apk; then
echo "FAILED: Android example failed to build."
failed_plugins+=("$plugin")
continue
fi

if ./gradlew testDebugUnitTest --info; then
echo "PASSED: Android unit tests for $plugin passed."
passed_plugins+=("$plugin")
else
echo "FAILED: Android unit tests for $plugin failed."
failed_plugins+=("$plugin")
fi
cd ../..
else
echo "SKIPPED: Android unit tests for $plugin don't exist. Skipping."
skipped_plugins+=("$plugin")
fi
;;
ios-test)
echo "=== Running iOS unit tests for $plugin ==="
if [ -d "ios/Tests" ]; then
XCODEBUILD_DESTINATION="platform=iOS Simulator,name=iPhone 11,OS=13.6"
if [ ! -d "example/ios" ]; then
echo "FAILED: example/ios missing, can't run tests."
failed_plugins+=("$plugin")
continue
fi
cd example/ios
if ! flutter build ios --no-codesign; then
echo "FAILED: iOS example failed to build."
failed_plugins+=("$plugin")
continue
fi

if xcodebuild test \
-workspace Runner.xcworkspace \
-scheme Runner \
-destination "$XCODEBUILD_DESTINATION" | xcpretty \
-r "junit" \
-o "test-results/$plugin-xcodebuild-test.xml"; then
echo "PASSED: iOS unit tests for $plugin passed."
passed_plugins+=("$plugin")
else
echo "FAILED: iOS unit tests for $plugin failed."
failed_plugins+=("$plugin")
fi
cd ../..
else
echo "SKIPPED: iOS unit tests for $plugin don't exist. Skipping."
skipped_plugins+=("$plugin")
fi
;;
esac
cd ..
echo
done

echo "=== Unit test complete ==="
echo

echo "${#passed_plugins[@]} passed plugins:"
printf "* %s\n" "${passed_plugins[@]}"
echo

echo "${#failed_plugins[@]} failed plugins:"
printf "* %s\n" "${failed_plugins[@]}"
echo

echo "${#skipped_plugins[@]} skipped plugins:"
printf "* %s\n" "${skipped_plugins[@]}"
echo

cd ..

set -e

return ${#failed_plugins[@]}
24 changes: 24 additions & 0 deletions packages/amplify_analytics_pinpoint/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,42 @@ android {

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
defaultConfig {
minSdkVersion 16
multiDexEnabled true
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
lintOptions {
disable 'InvalidPackage'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'com.android.support:multidex:1.0.3'
implementation "androidx.appcompat:appcompat:1.2.0"
implementation 'com.amplifyframework:aws-analytics-pinpoint:1.0.0'
implementation 'com.amplifyframework:aws-auth-cognito:1.0.0'

testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:3.1.0'
testImplementation 'org.robolectric:robolectric:4.3.1'

androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
}