如何让 Android Studio 将文件识别为源(测试) [英] How to get Android Studio to recognize file as source (test)
问题描述
我正在尝试为一个 android 项目创建 Robolectric 测试(哎呀,我什至很乐意让它们成为单元测试)
I'm trying to create Robolectric tests for an android project (heck, i'd be happy to even make them unit tests)
我的文件夹目录为:
MyApp
- app
- src
- main
- java
- com.myapp
HelloWorld
- test
- java
- com
- myapp
HelloWorldTest.java
问题是 HelloWorldTest.java
无法运行,因为它没有被识别为源.我如何设置它以便我可以运行这个类作为测试???????
The problem is that HelloWorldTest.java
can't be run because it's not being recognized as source. how do i set it up so that i can run this class as a test?????
如果我尝试执行 CMD + SHIFT + T
(创建测试的快捷方式),它会提示在与我的源文件相同的目录下创建测试,我不希望那样
if i try to do CMD + SHIFT + T
(shortcut for creating tests), it prompts to create the tests under the same directory as my source file and i do NOT want that
推荐答案
在 Android Studio 1.0 中,方案略有变化.
In Android Studio 1.0 the scheme has changed a little bit.
你的路径应该是 (app)/src/androidTest/java/com/myapp/HelloWorldTest.java
Your path should be (app)/src/androidTest/java/com/myapp/HelloWorldTest.java
以下是我在新的 Android Studio 项目中设置单元测试的方法:
Here's how I set up Unit Tests in a new Android Studio project:
- 在 Android Studio 中打开应用.
- 将项目资源管理器(左侧窗口)设置为显示项目"模式.点击左上角的小下拉菜单并选择项目".
- 右键单击src"目录,新建 -> 目录".
- 调用新目录androidTest
- 右键单击 androidTest 并添加一个java"目录.它将显示为绿色(表明它是一个测试 src 目录).
- 现在再次右键单击并添加一个包,例如com.mycompany.myapp.tests
- 添加一个扩展 AndroidTestCase 的新类.
现在添加一个新的测试配置:
Now add a new configuration for testing:
- 编辑配置
- 点击左上角的 +
- 选择 Android 测试
- 在常规"选项卡中选择您的主模块.
- 点击确定
现在您可以运行测试了.
Now you can run the tests.
这篇关于如何让 Android Studio 将文件识别为源(测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!