简体   繁体   English

带有JUnit测试的Android Studio

[英]Android Studio with JUnit testing

UPDATE UPDATE

According to the new build system , the hierarchy should look like: 根据新的构建系统 ,层次结构应如下所示:

MyProject/
  src/
      androidTest/
          java/
              com.example.app.test/
                  ... (source code for tests) ...
      main/
          AndroidManifest.xml
          java/
            com.example.app/
                ... (source code for main application) ...
          res/
              ... (resources for main application) ...

However, I don't have access to the package private classes in the com.example.app package. 但是,我无法访问com.example.app包中的包私有类。 The build.gradle android object looks like: build.gradle android对象看起来像:

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        testPackageName "com.example.app.test"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
        testFunctionalTest true
    }
}

I've also tried using the old structure, and get the error "Main Manifest missing from App/AndroidManifest.xml": 我也尝试使用旧结构,并获得错误“App / AndroidManifest.xml中缺少主要清单”:

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

    instrumentTest.setRoot('tests')
}

Do I need to use the old structure? 我需要使用旧结构吗? What's missing from the new structure that would put the tests in scope of the com.example.app package? 新结构中缺少哪些将测试放在com.example.app包的范围内?

UPDATE 2: 更新2:

I'm seeing an error in the instrumentTest folders AndroidManifest.xml file: "Cannot resolve symbol 'com.example.app'" in the element 我在instrumentTest文件夹AndroidManifest.xml文件中看到一个错误:“无法在元素中解析符号'com.example.app'”

 <instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.example.app"/>

This is the test class: 这是测试类:

package com.example.app.test;

import junit.framework.TestCase;

public class SimpleTest extends TestCase {

    public SimpleTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        assertTrue(true);
    }


    public void alwaysPasses() {

    }
}

I've also tried extending AndroidTestCase and received this error with both parent classes: 我也试过扩展AndroidTestCase并在两个父类中收到此错误:

!!! JUnit version 3.8 or later expected:

java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:185)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:168)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

I've tried resolving the error with this solution but there is no Android 1.6 Platform in the Project Structure | 我已尝试使用此解决方案解决错误,但项目结构中没有Android 1.6平台 Modules | 模块| Dependencies section. 依赖性部分。 So, basically, I don't know how to move the junit dependency above the Android dependency in the classpath. 所以,基本上,我不知道如何在类路径中将junit依赖项移到Android依赖项之上。

This problem was fixed by moving the test files into the same package that the (package-private) classes under test are in: 通过将测试文件移动到被测试的(包 - 私有)类所在的同一个包中来解决此问题:

MyProject/
  src/
      androidTest/
          java/
              com.example.app/
                  ... (source code for tests) ...
      main/
          AndroidManifest.xml
          java/
            com.example.app/
                ... (source code for main application) ...
          res/
              ... (resources for main application) ...

I had a similar issue, and I tracked the problem down to my gradle file. 我有一个类似的问题,我将问题跟踪到我的gradle文件。 I needed the following in there: 我需要以下内容:

aidl.srcDirs = ['src']

The location for the test classes is already declared for Gradle in the inner .iml file of the Android Studio project. 已在Android Studio项目的内部.iml文件中为Gradle声明测试类的位置。 For example, as of 0.8.3 you can see: 例如,从0.8.3开始,您可以看到:

      <sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/groovy" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
  <sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />

If you're still stuck with this I set up an example project that uses Espresso and Robotium test independently! 如果您仍然坚持这一点,我设置了一个独立使用Espresso和Robotium测试的示例项目!

https://github.com/hitherejoe/Android-Boilerplate https://github.com/hitherejoe/Android-Boilerplate

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

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