在Android Studio中创建单元测试 [英] Create unit tests in android studio

查看:365
本文介绍了在Android Studio中创建单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注教程.
我想做的是用AndroidJunitRunner创建单元测试.

I'm following this tutorial.
What I want to do is to create unit tests with AndroidJunitRunner.

我在app/src中的目录如下:

androidTest
debug
main
release

android docs建议我为单元测试创​​建一个test/java.
问题是,如果我处于android项目模式,则无法在android studio中创建新目录.我应该手动找到目录并手动创建.
有没有一种方法可以自动创建这些目录并针对一种口味进行测试?
我已阅读问题,该问题与之无关我的问题.

android docs suggests that I create a test/java for my unit tests.
The problem is that in android studio I can't create a new directory if I'm on android project mode. And I should find the directory manually and create it manually.
Is there a way to automatically create these directories and tests for one flavor?
I've read this question, which doesn't relate to my problem.

推荐答案

最简单的方法是在目录中手动创建文件夹. Android Studio会很快注意到它,它会显示在您的Android视图中.

It's easiest to just create the folder manually in the directory. Android Studio will notice it pretty quickly and it will show up in your Android view.

您也将需要在test/java文件夹中创建软件包.因此,如果包名称为com.foo.bar,则需要在app/src文件夹中创建以下目录结构:

You will need to create the packages in the test/java folder too. So if your package name is com.foo.bar you need to create the following dir structure in your app/src folder:

test/java/com/foo/bar

除了最新版本的Android Studio外,您无需再使用AndroidJunitRunner. Studio开箱即用地支持JUnit4测试.

As an aside in the latest versions of Android Studio you don't need to use AndroidJunitRunner anymore. Studio supports JUnit4 tests out of the box.

要对特定风味进行测试,您需要将风味名称附加到测试文件夹的末尾.因此,如果您的口味是dev,则文件夹结构将是:

To make tests for a particular flavor you need to append the flavor name to the end of the test folder. So if your flavor is dev then the folder structure would be:

testDev/java/com/foo/bar

这篇关于在Android Studio中创建单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆