使用 Clover on a Play 执行代码覆盖!使用 Ant 的框架应用程序

发布于 2024-11-13 10:23:26 字数 2468 浏览 3 评论 0 原文

我正在编写一个 Ant 脚本来执行一些额外检查 在我的 Play 上!框架应用。

目前,我通过 Ant 脚本执行测试,只需执行 exec 调用“播放自动测试”即可。

    <exec executable="${play.dir}/play.bat">
        <arg line="auto-test"/>
    </exec>

有人知道如何将 Clover 集成到 Play 测试套件中吗?显然,我不必使用上述方法来运行我的测试。

我还尝试使用执行 JUnit 测试的传统方式(即使用 Ant 的 junit 目标)编写 Ant 脚本,但遇到两个问题:

  • 执行所有测试时,只有第一个测试能够成功执行,而其他测试则执行成功会因为奇怪的原因失败
  • 如果我只公开套件中的一个测试并成功运行测试,它会说我的代码覆盖率为 0%。然后我以为我设置的三叶草不正确,但是,我创建了一个简单的类来测试一个不执行任何操作的生产类,并且覆盖率按照我的预期上升。

因此,如果我要沿着 junit 路线走下去,我需要知道如何执行所有测试,以便它们可以成功地运行一个又一个(它在使用运行 的 Play 方式时有效) >play auto-test),我需要知道为什么 Clover 似乎没有拾取 Play 测试涉及的代码行。

(我知道 Play 有一个 Cobertura 模块,但是,我发现 Clover 可以更好地告诉我准确的覆盖率数字)

更新:不幸的是,我无法复制我之前遇到的确切错误因为当我尝试手动编译时遇到了编译问题。我已经开始使用 Secure 模块,它只包含 Java 源文件。因此,在我的 Ant 脚本中,我调用 play precompile 来生成安全模块的字节代码(以及系统中的所有其他内容,包括我的代码)。因此,现在当我尝试使用 Clover 编译我的应用程序代码时,我认为编译器会陷入一些混乱,因为我的类有两个版本 - 一个由预编译命令(非 Clover)生成,另一个由我自己生成ant 编译(使用 clover):

[javac] C:\projects\testproject\out\clover\classes\clover8583527041716431332.tmp\model\HouseTest.java:45: incompatible types
[javac] found   : play.db.jpa.JPABase
[javac] required: models.House
[javac]         __CLR2_5_115y15ygoxiz3dv.R.inc(1527);House found = House.findById(id);

所以我现在基本上有两个问题:

  • 我需要能够编译我的源代码,该源代码也依赖于 Play 提供的模块(例如 CRUD、Secure),这些模块没有编译版本,因此我尝试绕过通过调用 play 来实现在我的 Ant 脚本中预编译
  • 一旦我开始编译工作,毫无疑问我会再次遇到原来的问题,即无法使用 junit 目标执行测试。

更新#2:事实证明,我得到的错误是由于 findById 调用需要从 JPABase 到 House 的转换(IDE 或游戏似乎并不关心)它)。因此,在我进入并为所有 play 的“find*”方法进行强制转换后,我实际上得到了 JUnit 和 Clover 报告! 但是...我现在遇到两种错误:

  • 在 Play 中创建的实体类可以通过扩展 Model 类来创建,该类提供了诸如以下的神奇方法前面提到的 find 方法以及 count 方法。 Model 超类实际上扩展了 GenericModel,它通过抛出 UnsupportedOperationException 来实现这些方法。很明显玩!在幕后做了一些更多的事情来提供这些方法的实际实现。不幸的是,我的测试(和生产代码)依赖于 count 等方法,但它们在我的 ant/junit 场景中抛出异常(注意:运行 play auto-test 时一切正常。

  • 我收到的另一个错误是由于我使用 Spring 模块这一事实造成的。在我的一个类中(根类),我打电话Spring.getBeanOfType(Some.class)。现在我使用自动扫描,但在 ant/junit 测试环境中,Spring 模块尚未设置我的 spring 容器,因此调用仅返回 null。

我有一种感觉,有一个神奇的修复程序可以解决我的这两个问题,但我不确定这个神奇的修复程序是什么。

I'm writing an Ant script to do some additional checks on my Play! Framework Application.

Currently, I am executing my tests from my Ant script by simply making an exec call to "play auto-test".

    <exec executable="${play.dir}/play.bat">
        <arg line="auto-test"/>
    </exec>

Would anyone know how to integrate Clover into a Play test suite? Obviously I am not tied to having to run my tests using the above.

I also tried writing the Ant script using the traditional way of executing JUnit tests (i.e. using Ant's junit target) and I got two problems:

  • When executing all my tests, only the first would execute successfully while the others would fail for strange reasons
  • If I just expose one test in my suite and have the test run successfully, it would say I have code coverage of 0%. I then thought I set up clover incorrectly, however, I created a simple class that tested a production class that did nothing and the coverage went up as I would expect.

So if I were to go down the junit route, I would need to know how to execute all my tests so that they can run one after another successfully (it works when using the Play way of running play auto-test) and I would need to know why Clover does not seem to pick up lines of code touched by Play tests.

(I know there is a Cobertura module for Play, however, I find that Clover does a better job telling me an accurate coverage figure)

Update: Unfortunately I am unable to replicate the exact error I was getting before as I have run into compilation issues when I try and compile things manually. I've started to use the Secure module and it only contains Java source files. So in my Ant script, I call play precompile which produces the byte code for the Secure module (as well as everything else in the system including my code). So now when I try and compile my app code using Clover, I think the compiler gets into a bit of a tangle as I have two versions of my classes - one produced by the precompile command (non-clover) and one produced by my own ant compilation (with clover):

[javac] C:\projects\testproject\out\clover\classes\clover8583527041716431332.tmp\model\HouseTest.java:45: incompatible types
[javac] found   : play.db.jpa.JPABase
[javac] required: models.House
[javac]         __CLR2_5_115y15ygoxiz3dv.R.inc(1527);House found = House.findById(id);

So I essentially have two problems now:

  • I need to be able to compile my source code that also depends on Play provided modules (e.g. CRUD, Secure) which do not have compiled versions hence my attempt at getting around it by calling play precompile myself in my Ant script
  • Once I get compilation working, I will undoubtedly have my original issue again of not being able to execute the tests using the junit target.

Update #2: It turns out that the error I got was due to the findById call required a cast from JPABase to House (not that the IDE or play seemed to care about it). So after I went in and put a cast for all of play's "find*" methods, I actually got JUnit and Clover reports! However... I am now getting two kinds of errors:

  • Entity classes created in Play can be created by extending the Model class which provides magic methods such as those find methods mentioned before as well as the count method. The Model superclass actually extends GenericModel which implements those methods by throwing an UnsupportedOperationException. So obviously Play! does some more magic behind the scenes to provide actual implementations of these methods. Unfortunately, my tests (and production code) rely on methods such as count but they are throwing the exception in my ant/junit scenario (note: everything works fine when running play auto-test.

  • The other error I am getting is due to the fact that I use the Spring module. In one of my classes (the root class), I call Spring.getBeanOfType(Some.class). Now I use auto-scanning, but in the ant/junit testing environment, the Spring module has yet to set up my spring container and therefore the call just returns null.

I have a feeling there is one magic fix that will resolve both of my issues however I am unsure what that magic fix is.

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

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

发布评论

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

评论(1

埖埖迣鎅 2024-11-20 10:23:26

Clover 进行源级检测,因此需要可用的源代码。在激活生成字节码的 clover 之前所做的一切都不会被“clovered”。

Clover for ant 会拦截 ant-compiler 调用,因此如果您在 ant 脚本中的任何其他编译任务之前执行 ,则所有内容都应由 clover 进行检测。

您可以以任何您想要的方式执行生成的编译代码,例如从脚本或从 junit 执行,这并不重要,只要代码被检测(当然 clover.jar 在类路径中可用)。
Clover 将 clover 数据库的位置硬编码到检测代码中,因此您在执行时不必指定任何内容。

如果您可以概述如何使用 clover,并且您还可以重新检查 http://confluence.atlassian.com/display/CLOVER/1.+QuickStart+Guide

Clover does source level instrumentation, so it needs source code available. Everything you do before activating clover that generates bytecode will not be "clovered".

Clover for ant intercepts ant-compiler calls, so if you do a <clover-setup/> before any other compilation tasks in your ant script, everything sould be instrumented by clover.

You can execute the resulting compiled code in any way you want, e.g. executing from script or from junit, it does not matter, as long as the code is instrumented (and of course clover.jar is available in the classpath).
Clover hard-codes the location of the clover-database into the instrumented code, so you do not have to specify anything when executing.

It would really help, if you could outline how you are using clover, and you could also do a recheck at clover documentation at http://confluence.atlassian.com/display/CLOVER/1.+QuickStart+Guide.

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