我一直在将 Robolectric 和 Ant 或 Maven 集成在一起,尽管 Ant 更可取,因为它更接近 Android SDK。
一切都在 eclipse 中工作。Robolectric 链接和单元测试运行,但是当我尝试从命令行运行和构建测试时,它似乎不起作用。
使用实际的 Robolectric 示例应用程序,我尝试同时进行 maven 和 ant 构建。
使用 ant 构建成功,但测试全部失败。
Maven 输出
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building RobolectricSample app 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.0-beta-1:enforce (enforce-maven) @ robolectricsample ---
[INFO]
[INFO] --- android-maven-plugin:3.0.0-alpha-13:generate-sources (default-generate-sources) @ robolectricsample ---
[INFO] ANDROID-904-002: Found aidl files: Count = 0
[INFO] ANDROID-904-002: Found aidl files: Count = 0
[INFO] C:\Program Files (x86)\Android\android-sdk\platform-tools\aapt.exe [package, -m, -J, C:\repos\Tests\robolectric-sample\target\generated-sources\r, -M, C:\repos\Tests\robolectric-sample\AndroidManifest.xml, -S, C:\repos\Tests\robolectric-sample\res, --auto-add-overlay, -I, C:\Program Files (x86)\Android\android-sdk\platforms\android-10\android.jar]
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ robolectricsample ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\repos\Tests\robolectric-sample\src\main\resources
[INFO] skip non existing resourceDirectory C:\repos\Tests\robolectric-sample\target\generated-sources\extracted-dependencies\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ robolectricsample ---
[INFO] Compiling 1 source file to C:\repos\Tests\robolectric-sample\target\classes
[INFO]
[INFO] --- android-maven-plugin:3.0.0-alpha-13:unpack (default-unpack) @ robolectricsample ---
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ robolectricsample ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\repos\Tests\robolectric-sample\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ robolectricsample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.6:test (default-test) @ robolectricsample ---
[INFO] Surefire report directory: C:\repos\Tests\robolectric-sample\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.pivotallabs.api.HttpTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.53 sec <<< FAILURE!
Running com.pivotallabs.tracker.TrackerAuthenticationRequestTest
Warning: an error occurred while binding shadow class: ShadowActivity
Warning: an error occurred while binding shadow class: ShadowActivityGroup
Warning: an error occurred while binding shadow class: ShadowApplication
Warning: an error occurred while binding shadow class: ShadowItemizedOverlay
Warning: an error occurred while binding shadow class: ShadowListActivity
Warning: an error occurred while binding shadow class: ShadowMapActivity
Warning: an error occurred while binding shadow class: ShadowPreferenceActivity
Warning: an error occurred while binding shadow class: ShadowService
Warning: an error occurred while binding shadow class: ShadowTabActivity
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.361 sec <<< FAILURE!
Running com.pivotallabs.tracker.RecentActivityTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec
Running com.pivotallabs.ViewEnablingTextWatcherTest
Tests run: 3, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 0.008 sec <<< FAILURE!
Running com.pivotallabs.api.ApiGatewayTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.006 sec <<< FAILURE!
Running com.pivotallabs.NamesAdapterTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.004 sec <<< FAILURE!
Running com.pivotallabs.NotifyDataSetChangedCallbacksTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.005 sec <<< FAILURE!
Running com.pivotallabs.GenericAdapterTest
Tests run: 4, Failures: 0, Errors: 4, Skipped: 0, Time elapsed: 0.01 sec <<< FAILURE!
Running com.pivotallabs.tracker.AuthenticationGatewayTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.004 sec <<< FAILURE!
Running com.pivotallabs.HomeActivityTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.004 sec <<< FAILURE!
Running com.pivotallabs.NamesActivityTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.005 sec <<< FAILURE!
Running com.pivotallabs.tracker.RecentActivitiesTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.004 sec <<< FAILURE!
Running com.pivotallabs.api.ApiResponseTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Running com.pivotallabs.injected.InjectedActivityTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.011 sec <<< FAILURE!
Running com.pivotallabs.MultiCallbacksTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Running com.pivotallabs.tracker.RecentActivityActivityTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.004 sec <<< FAILURE!
Running com.pivotallabs.tracker.RecentActivityRequestTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec
Running com.pivotallabs.ViewVisibleWhileOutstandingCallbacksTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.005 sec <<< FAILURE!
Running com.pivotallabs.tracker.SignInDialogTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.004 sec <<< FAILURE!
Running com.pivotallabs.views.LoadingTextViewTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 sec <<< FAILURE!
Running com.pivotallabs.tracker.RecentActivityAdapterTest
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.007 sec <<< FAILURE!
Running com.pivotallabs.util.StringsTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
Results :
Tests in error:
initializationError(com.pivotallabs.api.HttpTest)
shouldReturnUsernameAndPassword(com.pivotallabs.tracker.TrackerAuthenticationRequestTest)
shouldHaveUrl(com.pivotallabs.tracker.TrackerAuthenticationRequestTest)
shouldNotEnableTheViewIfTheAnyOfTheEditTextsContainWhitespace(com.pivotallabs.ViewEnablingTextWatcherTest)
shouldEnableTheViewWhenAllEditTextsHaveText(com.pivotallabs.ViewEnablingTextWatcherTest)
shouldDisableTheViewWhenOneOfTheEditTextsHaveEmptyText(com.pivotallabs.ViewEnablingTextWatcherTest)
initializationError(com.pivotallabs.api.ApiGatewayTest)
initializationError(com.pivotallabs.NamesAdapterTest)
onSuccessShouldNotifyDataSetChanged(com.pivotallabs.NotifyDataSetChangedCallbacksTest)
getCount_shouldReturnListSize(com.pivotallabs.GenericAdapterTest)
getItem_shouldReturnObjectAtIndex(com.pivotallabs.GenericAdapterTest)
getView_shouldCallThroughToGetView(com.pivotallabs.GenericAdapterTest)
getItemId_shouldReturnIndex(com.pivotallabs.GenericAdapterTest)
initializationError(com.pivotallabs.tracker.AuthenticationGatewayTest)
initializationError(com.pivotallabs.HomeActivityTest)
initializationError(com.pivotallabs.NamesActivityTest)
initializationError(com.pivotallabs.tracker.RecentActivitiesTest)
initializationError(com.pivotallabs.injected.InjectedActivityTest)
initializationError(com.pivotallabs.tracker.RecentActivityActivityTest)
shouldSetProgressBarVisibility(com.pivotallabs.ViewVisibleWhileOutstandingCallbacksTest)
initializationError(com.pivotallabs.tracker.SignInDialogTest)
initializationError(com.pivotallabs.views.LoadingTextViewTest)
shouldRecycleViews(com.pivotallabs.tracker.RecentActivityAdapterTest)
shouldCreateViews(com.pivotallabs.tracker.RecentActivityAdapterTest)
蚂蚁测试输出
test:
[junit] Warning: an error occurred while binding shadow class: ShadowActivity
[junit] Warning: an error occurred while binding shadow class: ShadowActivityGroup
[junit] Warning: an error occurred while binding shadow class: ShadowApplication
[junit] Warning: an error occurred while binding shadow class: ShadowListActivity
[junit] Warning: an error occurred while binding shadow class: ShadowMapActivity
[junit] Warning: an error occurred while binding shadow class: ShadowPreferenceActivity
[junit] Warning: an error occurred while binding shadow class: ShadowService
[junit] Warning: an error occurred while binding shadow class: ShadowTabActivity
[junit] Test com.pivotallabs.GenericAdapterTest FAILED
[junit] Test com.pivotallabs.HomeActivityTest FAILED
[junit] Test com.pivotallabs.NamesActivityTest FAILED
[junit] Test com.pivotallabs.NamesAdapterTest FAILED
[junit] Test com.pivotallabs.NotifyDataSetChangedCallbacksTest FAILED
[junit] Test com.pivotallabs.ViewEnablingTextWatcherTest FAILED
[junit] Test com.pivotallabs.ViewVisibleWhileOutstandingCallbacksTest FAILED
[junit] Test com.pivotallabs.api.ApiGatewayTest FAILED
[junit] Test com.pivotallabs.api.HttpTest FAILED
[junit] Test com.pivotallabs.injected.InjectedActivityTest FAILED
[junit] Test com.pivotallabs.tracker.AuthenticationGatewayTest FAILED
[junit] Test com.pivotallabs.tracker.RecentActivitiesTest FAILED
[junit] Test com.pivotallabs.tracker.RecentActivityActivityTest FAILED
[junit] Test com.pivotallabs.tracker.RecentActivityAdapterTest FAILED
[junit] Test com.pivotallabs.tracker.SignInDialogTest FAILED
[junit] Test com.pivotallabs.tracker.TrackerAuthenticationRequestTest FAILED
[junit] Test com.pivotallabs.views.LoadingTextViewTest FAILED
BUILD FAILED
C:\repos\Tests\robolectric-sample\build.xml:145: Unit test(s) failed. See reports!
示例的编译和链接似乎很好,但仍然有很多测试失败。我认为如果这些样本工作正常,它们应该 100% 成功。
任何人都可以阐明其中的奥秘吗?我是否可以在我的构建脚本中包含 junit 和 robolectric 测试,或者这是由于 android 构建系统几个月前进行了这些更改而没有得到正确支持的东西。