Java单元测试执行期间的方法调用列表
我遇到的情况是,我需要知道单个 JUnit 测试正在调用哪些方法。例如,如果我有以下伪代码:
public class UnitTest {
public main() {
Circle c = new Circle()
c.getArea()
}
}
public class Circle {
public Circle() {
...
}
public getArea() {
...
getRadius()
}
private getRadius() {
...
}
}
UnitTest 类的方法调用遵循此顺序(尽管出于我的目的,我不需要保留顺序,也不需要了解调用者的方法):
- UnitTest.main
- Circle.Circle
- Circle.getArea
- Circle.getRadius
本质上,我想知道 JUnit 测试中实际执行了程序的哪一部分。我想我可以动态地解决这个问题,但我很难找到工具或方法。我发现的一些工具的主要问题是它们通常非常直观(需要用户交互来提取所需的数据)。我正在寻找一些只提供方法列表(xml、文本等...)而没有 GUI 方面的东西。最后,我尝试通过脚本方法自动化测试套件。
我考虑使用的方法是:
- 使用 Java 代理在每次方法调用时打印出 class.method 。
- 使用方面进行跟踪像这样
如果有一个工具已经可以做到这一点那就太好了。如果没有,我似乎正在使用我指定的方法之一来解决我的问题。
任何帮助/建议将不胜感激。
更新已解决 我决定使用 Emma 和以下命令来提取方法调用的信息(只需要解析覆盖率超过 0% 的任何方法的报告):
emmarun -r xml // To output to XML
-Dreport.sort=-method // Sort method coverage in descending order
-Dverbosity.level=silent // Minimize script output
-Dreport.metrics=method:1 // Flag any method with a 0% coverage
-Dreport.columns=method,name // Only show the method and name columns
-Dreport.depth=method // Consider the data of method coverage
I have a situation where I need to know what methods are being called from a single JUnit test. For example, if I have the following pseudo-code:
public class UnitTest {
public main() {
Circle c = new Circle()
c.getArea()
}
}
public class Circle {
public Circle() {
...
}
public getArea() {
...
getRadius()
}
private getRadius() {
...
}
}
The method calls of the UnitTest class follow this order (though for my purposes I don't need to preserve order, or to know of the caller's method):
- UnitTest.main
- Circle.Circle
- Circle.getArea
- Circle.getRadius
Essentially, I want to know what part of the program is actually being exercised in the JUnit tests. I figure I can dynamically figure this out, but I am having trouble finding a tool or approach. The main problem with some of the tools I have found is that they are often very visual (require user interaction to extract the required data). I am looking for something that gives me just a list of the methods (xml, text, etc...) without the GUI aspect. In the end I am trying to automate this for test suites via a scripting approach.
The approaches I am thinking of using would be either:
- Using a Java Agent to print out the class.method at every method call.
- Tracing using Aspects like this
If there is a tool which already does this that would be great. If not, does it seem like I am on track to solve my problem using one of the approaches I've specified.
Any help/suggestions would be appreciated.
UPDATE-SOLVED
I decided to use Emma with the following commands to extract the information of method calls (just need to parse report for any method over a 0% coverage):
emmarun -r xml // To output to XML
-Dreport.sort=-method // Sort method coverage in descending order
-Dverbosity.level=silent // Minimize script output
-Dreport.metrics=method:1 // Flag any method with a 0% coverage
-Dreport.columns=method,name // Only show the method and name columns
-Dreport.depth=method // Consider the data of method coverage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否正在寻找“覆盖”工具,例如 Emma 或 Cobertura?
Are you looking for a "Coverage" tool, such as Emma or Cobertura?
如果您想知道您的测试调用了哪些方法
您可以使用代码覆盖率工具,例如 cobertura 或 艾玛。
它们都提供了与 Maven、ant 和 Eclipse 的良好集成
并且可以生成 xml 或 html 报告。
If you want to know which methods are called by your test
you can use a code coverage tools like cobertura or emma.
Both of them provide a great integration with maven ,ant and eclipse
and can produce a xml or html report.