艾玛报告覆盖率为 0%

发布于 2024-11-11 17:31:01 字数 2253 浏览 7 评论 0原文

我想在运行单元测试时获得代码覆盖率。 我使用标准 android build.xml 运行 ant 覆盖率进行测试。

测试运行良好。 antcoverage 的最后一个字符串是

Tests run: 59,  Failures: 1,  Errors: 4

Generated code coverage data to /data/data/my.package/files/coverage.ec

但是coverage.ec 文件只有 37 个字节长,几乎是空的。

运行 emma report 会告诉你

no collected coverage data found in any of the data files [all reports will be empty]

并生成漂亮的报告,每个字段都为零。

我认为 emma 应该产生更大的覆盖率。

我做错了什么?

---更新---

做了一些深入的挖掘。看起来除了覆盖结果生成之外,大多数事情都很好。

1) 它编译所有内容

[javac] /blabla/android-sdk-linux_x86/tools/ant/main_rules.xml:384: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 88 source files to /blabla/project/tests/instrumented/classes
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.

2) 它正在为正在测试的项目使用 mode="overwrite" 执行 。路径没问题。

-艾玛仪器: [echo] 从 /blabla/project/tests/instrumented/classes 检测类...

结果,有一个包含 98 个类元数据的 *.em 文件。

3)一些标准的android转换为dex,打包为未对齐,zip对齐。结果是/blabla/project/tests/instrumented/project-debug.apk。

4)将此project-debug.apk安装到模拟器上。

5) 编译测试项目。 编译: [javac] /blabla/android-sdk/android-sdk-linux_x86/tools/ant/main_rules.xml:384: 警告: 'includeantruntime' 未设置,默认为 build.sysclasspath=last;对于可重复构建设置为 false [javac] 将 110 个源文件编译到 /blabla/project/tests/bin/classes

源文件包括所有以前的文件加上测试(110 = 88 + 测试),如 build.properties 中所述(多个 source.dir 用“;”分隔) ”)。

6) 资源、Dex、签名、zip 对齐...结果是projectTest-debug.apk

7) 将projectTest-debug.apk 安装到模拟器上。

8) 通过指定“coverage on”的 am 运行测试。它表明

[exec] Generated code coverage data to /data/data/blabla.project/files/coverage.ec

9) 此coverage.ec 不包含相关数据。它的长度为 37 字节。报告显示,

processing input file [/home/ubuntu/projects/ppf2/workspace/PPF2/tests/coverage.ec] ...
loaded 0 coverage data entries
...
no collected coverage data found in any of the data files [all reports will be empty]

除了最后一步之外,一切似乎都对我有利。

I want to get code coverage when running unit tests.
I run ant coverage using standard android build.xml for tests.

Tests run well. The last strings from ant coverage are

Tests run: 59,  Failures: 1,  Errors: 4

Generated code coverage data to /data/data/my.package/files/coverage.ec

But the coverage.ec file is only 37 bytes long and is almost empty.

Running emma report on it tells

no collected coverage data found in any of the data files [all reports will be empty]

and generates beautiful report with ZEROES in every field of it.

I suppose that emma should generate a bigger coverage.ec.

What am I doing wrong?

--- update ---

Did some deep digging. It seems that most of things are well except coverage result generation.

1) It compiles everything saying

[javac] /blabla/android-sdk-linux_x86/tools/ant/main_rules.xml:384: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 88 source files to /blabla/project/tests/instrumented/classes
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.

2) It is doing <instr> with mode="overwrite" for the project that is under test. The path is ok.

-emma-instrument:
[echo] Instrumenting classes from /blabla/project/tests/instrumented/classes...

As a result, there is a *.em file with metadata for 98 classes.

3) Some standard android conversion to dex, package to unaligned, zip align. Result is /blabla/project/tests/instrumented/project-debug.apk.

4) Installing this project-debug.apk onto emulator.

5) Compiling the tests project.
compile:
[javac] /blabla/android-sdk/android-sdk-linux_x86/tools/ant/main_rules.xml:384: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 110 source files to /blabla/project/tests/bin/classes

Source files include all the previous files plus tests (110 = 88 + tests), as stated in build.properties (multiple source.dir separated by ";").

6) Resources, Dex, signing, zip align... Result is projectTest-debug.apk

7) Installing projectTest-debug.apk onto emulator.

8) Running tests through am where "coverage on" specified. It tells that

[exec] Generated code coverage data to /data/data/blabla.project/files/coverage.ec

9) This coverage.ec contains no relevant data. It is 37 bytes long. Report on it tells that

processing input file [/home/ubuntu/projects/ppf2/workspace/PPF2/tests/coverage.ec] ...
loaded 0 coverage data entries
...
no collected coverage data found in any of the data files [all reports will be empty]

Everything seems good for me except the last step.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-11-18 17:31:01

我遇到了同样的问题,我想我明白这里发生了什么。

我看起来被测试的包的源也在测试包的内部(或被引用为源)。

结果是,如果您打开它们:

PackageUnderTest/ClassFileUnderTest

TestPackage/TestClass
TestPackage/ClassFileUnderTest

ClassFileUnderTest:

pluclic class ClassFileUnderTest{
    public int foo(){
       ...
    }
}

TestClass:

pluclic class TestClass{
    public void testFoo(){
       int result = foo();
       assert...
    }
}

这里发生的是,每当你的 TestClass 从 testFoo() 调用 foo() 方法时, foo() 就会在 TestClass 上被调用/ClassFileUnderTest 而不是 PackageUnderTest/ClassFileUnderTest。

因此,仪表化的 PackageUnderTest/ClassFileUnderTest 从未运行,也没有在覆盖率报告中进行统计。

从 TestPackage 中删除引用 PackageUnderTest 代码会强制 PackageUnderTest 代码从 PackageUnderTest 运行并计入覆盖率报告中。

I ran into the same issue and I think I understand what happened here.

I looks like the sources of the package under test were also inside (or referenced as being sources) of the test package.

The result is that your package apks looked like the following if you opened them up:

PackageUnderTest/ClassFileUnderTest

TestPackage/TestClass
TestPackage/ClassFileUnderTest

ClassFileUnderTest:

pluclic class ClassFileUnderTest{
    public int foo(){
       ...
    }
}

TestClass:

pluclic class TestClass{
    public void testFoo(){
       int result = foo();
       assert...
    }
}

What is happening here is that whenever your TestClass called the foo() method from testFoo(), foo() got called on TestClass/ClassFileUnderTest instead of PackageUnderTest/ClassFileUnderTest.

As a result, the instumented PackageUnderTest/ClassFileUnderTest never got run and didn't get tallied in the coverage report.

Removing the reference PackageUnderTest code from the TestPackage enforced that the PackageUnderTest code got run from PackageUnderTest and tallied into the coverage report.

樱桃奶球 2024-11-18 17:31:01

最终,经过几个小时的奋战,问题得到了解决。
解决办法非常简单而且出人意料。

在 TEST 项目的 build.properties 中,我有类似的内容:

tested.project.dir=..
env.WORKSPACE= /bla/bla
source.dir=${env.WORKSPACE}/first/src;${env.WORKSPACE}/second/src;${env.WORKSPACE}/andsoon/src;

但是!我不应该在这里指定 source.dir !指定 tested.project.dir 足以成功编译测试项目。

此外,如果我像这样在 TEST 项目中指定 source.dir - 测试运行良好,但 emma 报告覆盖率为零,正如问题中所述。

Finally, after many hours of fighting, question resolved.
Resolution is very simple and unexpectable.

In build.properties of the TEST project I had something like:

tested.project.dir=..
env.WORKSPACE= /bla/bla
source.dir=${env.WORKSPACE}/first/src;${env.WORKSPACE}/second/src;${env.WORKSPACE}/andsoon/src;

But! I should NOT specify source.dir here! Specifying tested.project.dir is enough to compile test project successfully.

Moreover, if I specify source.dir in TEST project like this - tests run well but emma reports zero coverage, just as stated in question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文