简体   繁体   English

Android 测试依赖于库项目上的测试类

[英]Android tests depends on test classes on a library project

so my project has two modules:所以我的项目有两个模块:

  • app应用程序
  • library图书馆

In the addroidTest directory of the app, I have some classes that depends on classes located in the addroidTest directory of the library.在应用程序的addroidTest目录中,我有一些类依赖于位于库的addroidTest目录中的类。

The structure is:结构是:

:library /src/main
            /androidTests

:app     /src/main
            /androidTests

They compile finely in Android Studio, but when I run the tests I get:它们在 Android Studio 中编译得很好,但是当我运行测试时,我得到:

Error:(6, 32) error: cannot find symbol class XXXX Error:Execution failed for task [...]错误:(6, 32) 错误:找不到符号类 XXXX 错误:任务 [...] 执行失败

Compilation failed;编译失败; see the compiler error output for details.有关详细信息,请参阅编译器错误输出。

[....] [....]

Execution failed for task > ':app:compileProductionDebugAndroidTestJavaWithJavac'.任务 > ':app:compileProductionDebugAndroidTestJavaWithJavac' 执行失败。

It seems that when gradle executes the task ':app:compileProductionDebugAndroidTestJavaWithJavac' it cannot find classes located in the test directory of the library.似乎当 gradle 执行任务 ':app:compileProductionDebugAndroidTestJavaWithJavac' 时,它找不到位于库的 test 目录中的类。

Any idea how can I solve this?知道我该如何解决这个问题吗?

test source sets are not added to the outputted jar and therefore do not exist when app looks for library code in androidTest . test源集不会添加到输出的jar ,因此当appandroidTest查找library代码时不存在。 You can place your shared classes in the main lib code and reference it from your app there, but I don't think it's possible for you to depend|extend on test classes of another module.您可以将共享类放在主库代码中,并在那里从您的app引用它,但我认为您不可能依赖|扩展另一个模块的测试类。

I've worked with Junit and it was located in libs folder as you mentioned.我曾与 Junit 合作过,它位于您提到的 libs 文件夹中。 for your test classes you can try these steps:对于您的测试类,您可以尝试以下步骤:

1- in androidTest folder, make sure you place your test classes in a path like this: androidTest -> java -> com.examples.bla (the same as the project) ->the test class. 1- 在 androidTest 文件夹中,确保将测试类放在这样的路径中:androidTest -> java -> com.examples.bla(与项目相同)-> 测试类。

2- Go to run/debug configuration ->defaults -> Android Tests (add it if it's not there), then choose "All in Module" for test option, and also make sure that Gradle-aware Make is added at the bottom of the configuration window. 2-转到运行/调试配置->默认值-> Android测试(如果不存在则添加它),然后选择“模块中的全部”作为测试选项,并确保在底部添加了Gradle-aware Make配置窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM