Android Studio 3.0 gradle 3.0.0-beta2,打破了 Kotlin 单元测试的覆盖范围? [英] Android Studio 3.0 gradle 3.0.0-beta2, breaks Kotlin Unit Test Coverage?

查看:39
本文介绍了Android Studio 3.0 gradle 3.0.0-beta2,打破了 Kotlin 单元测试的覆盖范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Kotlin 类,如下

I have a simple Kotlin classes, as below

class MyClass {
    fun justSayHello(yes: Boolean): String {
        if (yes) {
            return "Hello"
        } else {
            return "Sorry"
        }
    }
}

我有我的测试(这里用 Java 编写,也可以用 Kotlin 编写)

I have my test (written in Java here, could be in Kotlin, also)

public class MyClassTest {
    private MyClass myClass = new MyClass();

    @Test
    public void testFirst() {
        myClass.justSayHello(true);
    }

    @Test
    public void testSecond() {
        myClass.justSayHello(false);
    }
}

当我使用 classpath 'com.android.tools.build:gradle:3.0.0-beta2' 在 Android Studio 3.0 Beta-2 中使用 Coverage 运行测试时,没有报告它的覆盖范围.

When I run a test with Coverage in Android Studio 3.0 Beta-2 using classpath 'com.android.tools.build:gradle:3.0.0-beta2', no coverage is reported for it.

但是当我使用 classpath 'com.android.tools.build:gradle:2.3.3' 运行测试时,报告了 100% 的覆盖率.

But when I run the test using classpath 'com.android.tools.build:gradle:2.3.3', 100% coverage reported.

当我将源代码更改为 Java 时:

When I change my source code to Java:

public class MyClass {
    public String justSayHello(boolean yes) {
        if (yes) {
            return "Hello";
        } else {
            return "Sorry";
        }
    }
}

它适用于两个 gradle 构建工具

It works fine for both gradle build tools

在我看来 'com.android.tools.build:gradle:3.0.0-beta2' 对 Kotlin 的测试覆盖率测量有问题.

It seems to me that 'com.android.tools.build:gradle:3.0.0-beta2' has the broken test coverage measurement for Kotlin.

我错过了什么吗?有没有办法让我在 Kotlin 中获得测试覆盖率?

Did I miss anything? Is there a workaround for me to get the test coverage in Kotlin?

推荐答案

如果有人仍在寻找解决方案,则添加 gradle 任务以将案例从 tmp 目录复制到覆盖输出查找的目录中将此问题作为解决方法.

In case anyone is still looking for a solution, adding in a gradle task to copy the cases from the tmp directory into the directory that the coverage output looks in helps with this issue as a workaround.

例如将 copyTestClasses 添加到您的模块 gradle 文件中

For example add copyTestClasses to your module gradle file

task copyTestClasses(type: Copy) {
    from "build/tmp/kotlin-classes/debug"
    into "build/intermediates/classes/debug"
}

然后在运行测试之前设置默认值以运行 gradle 任务

And then setting up your defaults to run the gradle task before running the tests

在尝试使用 gradle 指向它们之前手动查找项目中的两个目录会有所帮助,以确保您指向正确的位置(风味会改变您需要指向的目录)

It can help to find both of the directories in your project manually before trying to point to them using gradle, to make sure that you're pointing to the right place (flavours will change the directories that you need to point to)

这篇关于Android Studio 3.0 gradle 3.0.0-beta2,打破了 Kotlin 单元测试的覆盖范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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