JUnit In Action读书笔记(2)

1.4 Setting up JUnit
    JUnit comes in the form of a jar file (junit.jar). In order to use JUnit to write your application tests, you'll simply need to add the junit jar to your project's complication classpath and to your execution classpath when you run the tests.

    用一个新解压的junit3.8.1.jar来实验.(也不知以前解压的为什么不行,老是报错)

    THE GRAPHICAL TEST RUNNER ALSO USES ITS OWN CLASSLOADER INSTANCE(这个CLASSLOADER INSTANCE是此JUNIT.SWINGUI.TESTRUNNER APPLICATION自定义的么?看E:\JUNIT3.8.1\SRC\JUNIT\SWINGUI下的源代码,却没有发现有什么CLASSLOADER INSTANCE类的定义)(A RELOADING CLASSLOADER).This makes it easier to use interactively(有了这个its own classloader怎么就可以use interactively了呢?), because you can reload classes(after changing them)(这里的classes是想要测试的那些类?) and quickly run the test again without restarting the test runner.(那这样的without restarting the test runner在自己的开发中又怎么用上呢?对这个classloader可真是知道的太少了!)

1.5 Testing with JUnit
    many features that make easier to write and to run:
        Separate classloader for each unit test to avoid side effects.
        Standard resource initialization and reclamation methods(setUp and tearDown)
        A variety of assert methods to make it easy to check the results of your tests.
    let's see what the simple Calculator test looks like when written with JUnit:

        import junit.framework.TestCase;
        public class TestCalculator extends TestCase{    // ---(1)
            public void testAdd(){                                        // --(2)
                Calculatro c = new Calculator();                    // --(3)
                double r = c.add(10,50);                                // --(4)
                assertEquals(60,result,0);                                // --(5)
            }
        }
    At (5), the JUnit  framework begins to shine!To check the result of the test,you call an assertEquals method,which you inherited from the base TestCase.
    /**
     * Asserts that two doubles are equal concerning a delta. If the expected
     * value is infinity then the delta value is ignored.
     */
    static public void assertEquals(double expected, double actual, double delta) {
        assertEquals(null, expected, actual, delta);
    }

    Which brings us to the mysterious delta parameter.Most often,the delta parameter can be zero,and you can safely ignore it.It comes into play calculations that are not always precise,which includes many floating-point calculations.The delta provides a plus/minus factor.So if the actural is within the range (expected-delta) and (expected+delta),the test will sill pass.



Chapter 2 Exploring JUnit

猜你喜欢

转载自rmn190.iteye.com/blog/177820