使用 junit4 进行单元测试

发布于 2024-09-05 05:45:29 字数 25 浏览 2 评论 0原文

我如何在junit4中创建测试套件?

how do i create a test suite in junit4 ??

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

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

发布评论

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

评论(2

空心↖ 2024-09-12 05:45:29

这是一个例子:

@RunWith(Suite.class)
@Suite.SuiteClasses(
        {
            TestClass1.class,
            TestClass2.class,
        })
public class DummyTestSuite
{

}

Here is one example:

@RunWith(Suite.class)
@Suite.SuiteClasses(
        {
            TestClass1.class,
            TestClass2.class,
        })
public class DummyTestSuite
{

}
桜花祭 2024-09-12 05:45:29

单元测试非常简单,并且通过一个简单的示例可以得到最好的解释。

我们将使用以下类计算数组的平均值:

package com.stackoverflow.junit;

public class Average {
    public static double avg(double[] avg) {
        double sum = 0;

        // sum all values
        for(double num : avg) {
            sum += num;
        }


        return sum / avg.length;
    }
}

我们的 JUnit 测试现在将测试此方法的一些基本操作:

package com.stackoverflow.junit;

import junit.framework.TestCase;

public class AverageTest extends TestCase {
    public void testOneValueAverage() {
        // we expect the average of one element (with value 5) to be 5, the 0.01 is a delta because of imprecise floating-point operations
        double avg1 = Average.avg(new double[]{5});
        assertEquals(5, avg1, 0.01);

        double avg2 = Average.avg(new double[]{3});
        assertEquals(3, avg2, 0.01);        
    }

    public void testTwoValueAverage() {
        double avg1 = Average.avg(new double[]{5, 3});
        assertEquals(4, avg1, 0.01);

        double avg2 = Average.avg(new double[]{7, 2});
        assertEquals(4.5, avg2, 0.01);      
    }

    public void testZeroValueAverage() {
        double avg = Average.avg(new double[]{});
        assertEquals(0, avg, 0.01);
    }
}

前两个测试用例将表明我们正确实现了该方法,但最后一个测试用例将失败。
但为什么?
数组的长度为零,我们正在除零。浮点数除以零不是数字 (NaN),也不是零。

Unit Testing is really easy and best explained on a simple example.

We'll have the following class calculating the average of an array:

package com.stackoverflow.junit;

public class Average {
    public static double avg(double[] avg) {
        double sum = 0;

        // sum all values
        for(double num : avg) {
            sum += num;
        }


        return sum / avg.length;
    }
}

Our JUnit test will now test some basic operations of this method:

package com.stackoverflow.junit;

import junit.framework.TestCase;

public class AverageTest extends TestCase {
    public void testOneValueAverage() {
        // we expect the average of one element (with value 5) to be 5, the 0.01 is a delta because of imprecise floating-point operations
        double avg1 = Average.avg(new double[]{5});
        assertEquals(5, avg1, 0.01);

        double avg2 = Average.avg(new double[]{3});
        assertEquals(3, avg2, 0.01);        
    }

    public void testTwoValueAverage() {
        double avg1 = Average.avg(new double[]{5, 3});
        assertEquals(4, avg1, 0.01);

        double avg2 = Average.avg(new double[]{7, 2});
        assertEquals(4.5, avg2, 0.01);      
    }

    public void testZeroValueAverage() {
        double avg = Average.avg(new double[]{});
        assertEquals(0, avg, 0.01);
    }
}

The first two test cases will show that we implemented the method correct, but the last test case will fail.
But why?
The length of the array is zero and we are diving by zero. A floating point number divided by zero is not a number (NaN), not zero.

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