确定哪些测试用例涵盖了一个方法 [英] Determining which test cases covered a method

查看:161
本文介绍了确定哪些测试用例涵盖了一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的当前项目要求我编写一个在Web应用程序上运行功能测试的工具,并输出方法覆盖率数据,记录哪个测试用例遍历了哪个方法。

The current project I'm working on requires me to write a tool which runs functional tests on a web application, and outputs method coverage data, recording which test case traversed which method.

详细信息:
测试中的Web应用程序将是在servlet容器(例如Tomcat)中运行的Java EE应用程序。功能测试将使用JUnit在Selenium中编写。一些方法将被注释,以便在部署到测试环境之前对它们进行检测。执行Selenium测试后,将记录带注释的方法的执行。

Details: The web application under test will be a Java EE application running in a servlet container (eg. Tomcat). The functional tests will be written in Selenium using JUnit. Some methods will be annotated so that they will be instrumented prior to deployement into the test enviornment. Once the Selenium tests are executed, the execution of annotated methods will be recorded.

问题:这个项目的一大障碍是找到一种方法将测试用例的执行与方法的遍历联系起来,尤其是测试和应用程序在不同的JVM上运行,并且无法在应用程序中传输测试用例的名称,也无法使用线程信息将测试与代码执行联系起来。

Problem: The big obstacle of this project is finding a way to relate an execution of a test case with the traversal of a method, especially that the tests and the application run on different JVMs, and there's no way to transmit the name of the test case down the application, and no way in using thread information to relate test with code execution.

建议的解决方案:我的解决方案将包括使用执行时间:我将JUnit框架扩展到记录测试用例执行的时间,我检测应用程序,以便节省遍历方法的时间。我尝试使用相关性将测试用例与方法覆盖率联系起来。

Proposed solution: My solution would consist of using the time of execution: I extend the JUnit framework to record the time the test case was executed, and I instrument the application so that it saves the time the method was traversed. And I try to use correlation to link the test case with method coverage.

预期问题:此解决方案假定测试用例按顺序执行,并且测试用例在下一个测试用例开始之前结束。 JUnit的这个假设是否合理?

Expected problems: This solution assumes that test cases are executed sequentially, and a test case ends befores the next one starts. Is this assumption reasonable with JUnit?

问题:简单地说,我可以就建议的解决方案提出您的意见,也许还有关于如何改进的建议并使其在大多数Java EE应用程序上更强大和更强大?或者导致已经实施的解决方案?

Question: Simply, can I have your input on the proposed solution, and perhaps suggestions on how to improve and make it more robust and functional on most Java EE applications? Or leads to already implemented solutions?

谢谢

编辑:添加更多要求,该工具应该能够在任何Java EE应用程序上工作,并且需要在应用程序中进行最少量的配置或更改。虽然我知道这不是一个现实的要求,但该工具至少不需要对应用程序本身进行任何大的修改,例如添加类或代码行。

Edit: To add more requirements, the tool should be able to work on any Java EE application and require the least amount of configuration or change in the application. While I know it isn't a realistic requirement, the tool should at least not require any huge modification of the application itself, like adding classes or lines of code.

推荐答案

您提出的解决方案似乎是合理的解决方案,除了通过计时将测试和请求联系起来的建议解决方案。我之前尝试过做这种事情,但它确实有效。大多数时候。除非你非常仔细地编写JUnit代码,否则你会遇到很多问题,因为两台机器之间的时间不同,或者如果你只有一台机器,只需匹配一台机器。

Your proposed solution seems like a reasonable one, except for the proposed solution to relate the test and request by timing. I've tried to do this sort of thing before, and it works. Most of the time. Unless you write your JUnit code very carefully, you'll have lots of issues, because of differences in time between the two machines, or if you've only got one machine, just matching one time against another.

更好的解决方案是实现 Tomcat Valve ,您可以将其插入到webapp的server.xml的生命周期中。阀门的优势在于您可以在server.xml中定义它们,因此您根本不会触及Web应用程序。

A better solution would be to implement a Tomcat Valve which you can insert into the lifecycle in the server.xml for your webapp. Valves have the advantage that you define them in the server.xml, so you're not touching the webapp at all.

您需要实现invoke()。最好的起点可能是 AccessLogValve 。这是AccessLogValve中的实现:

You will need to implement invoke(). The best place to start is probably with AccessLogValve. This is the implementation in AccessLogValve:

/**
 * Log a message summarizing the specified request and response, according
 * to the format specified by the <code>pattern</code> property.
 *
 * @param request Request being processed
 * @param response Response being processed
 *
 * @exception IOException if an input/output error has occurred
 * @exception ServletException if a servlet error has occurred
 */
public void invoke(Request request, Response response) throws IOException,
        ServletException {

    if (started && getEnabled()) {                
        // Pass this request on to the next valve in our pipeline
        long t1 = System.currentTimeMillis();

        getNext().invoke(request, response);

        long t2 = System.currentTimeMillis();
        long time = t2 - t1;

        if (logElements == null || condition != null
                && null != request.getRequest().getAttribute(condition)) {
            return;
        }

        Date date = getDate();
        StringBuffer result = new StringBuffer(128);

        for (int i = 0; i < logElements.length; i++) {
            logElements[i].addElement(result, date, request, response, time);
        }

        log(result.toString());
    } else
        getNext().invoke(request, response);       
}

所有这一切都记录了您访问它的事实。

All this does is log the fact that you've accessed it.

您将实现一个新的Valve。对于您的请求,您将传递唯一ID作为URL的参数,该ID用于标识您正在运行的测试。你的阀门会在invoke()之前和之后完成所有繁重的工作。如果需要,您可以删除删除getNext()。invoke()的唯一参数。

You would implement a new Valve. For your requests you pass a unique id as a parameter for the URL, which is used to identify the tests that you're running. Your valve would do all of the heavy lifting before and after the invoke(). You could remove remove the unique parameter for the getNext().invoke() if needed.

要测量覆盖率,您可以使用JB Nizet建议的覆盖工具,基于你传递的唯一身份证。

To measure the coverage, you could use a coverage tool as suggested by JB Nizet, based on the unique id that you're passing over.

所以,从junit,如果你的原始电话是

So, from junit, if your original call was

@Test void testSomething() {
    selenium.open("http://localhost/foo.jsp?bar=14");
}

您可以将其更改为:

@Test void testSomething() {
    selenium.open("http://localhost/foo.jsp?bar=14&testId=testSomething");
}

然后你在阀门中拿起参数testId。

Then you'd pick up the parameter testId in your valve.

这篇关于确定哪些测试用例涵盖了一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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