`

初学junit笔记一

阅读更多

在junit3.8中,测试方法满足如下原则:

1)public的

2)void的

3)无方法参数

4)方法名称必须以test开头

5)继承自TestCase

 

出名的语句:keep the bar green to keep the code clean

 

测试一个方法会在某种情况下抛出异常的方法:

public void testDivide2()

{

Throwable tx = null;

try

{

Calculator cal = new Calculator();

cal.divide(3,0);

Assert.fail();         //Junit程序执行到这个语句时会直接终止testCase的往下执行,用来验证不会执行到这个语句

}catch(Exception e)

{

tx = e;

}

Assert.assertNotNull(tx);

Assert.assertEquals(Exception.class , tx.getClass());

Assert.assertEquals("除数不能为0" , tx.getMessage());

}

 

public void testDivide1()

{

int result = 0;

try

{

Calculator cal = new Calculator();

result = cal.divide(6,4);

}catch(Exception e)

{

Assert.fail();         //Junit程序执行到这个语句时会直接终止testCase的往下执行,用来验证不会执行到这个语句

}

Assert.assertEquals(1 , result);

}

 

 

千万不要将要测试的方法抛出的异常在TestCase中不捕获,继续往上抛

public void testDivide2() throws Exception      //不要这样写测试用例

 

不要写重复的代码(Don't Repeat yourself)

 

setUp方法是在每一个TestCase执行之前都会执行的方法,而tearDown是每一个TestCase执行之后都会执行的方法

那么有没有一种方法是在所有的TestCase执行之前执行,并且只执行一次,又有没有另一种方法是在所有的TestCase执行完后再执行的呢?在JUnit3.8里是没有提供这样的方法的,但在JUnit4中已经支持了这种方法

 

用命令行的方式执行TestCase:

不用Eclipse等工具时,TestCase应该怎样去运行呢?

1、命令行方式(适用于和ant整合)

public static void main(String[] args)

{

    junit.textui.TestRunner.run(CalculatorTest.class);

}

 

2、swing图形化的方式

public static void main(String[] args)

{

    junit.swingui.TestRunner.run(CalculatorTest.class);

}

 

3、awt图形化的方式

public static void main(String[] args)

{

    junit.awtui.TestRunner.run(CalculatorTest.class);

}

 

如何一次运行多个TestCase类??

public class TestAll extends TestCase

{

    public static Test suite()

    {

        TestSuite suite = new TestSuite();

        suite.addTestSuite(CalculatorTest.class);

        suite.addTestSuite(LargestTest.class);

 

        return suite;

    }

}

public static Test suite()也是类型于TestCase一样可以直接运行的方法,运行它就可以一次性将多个TestCase类一次性运行了

public static Test suite()这个方法的签名是固定的,不能有任何改变

TestSuite里不仅可以包含TestCase测试类,还可以包含TestSuite测试类,如有另一个类有如下语句将上面的suite包含在一起:

suite.addTestSuite(TestAll.class);

suite.addTestSuite(OtherSuite.class);

这里使用了组合模式

 

Junit4

不需要继承自TestCase

setUp的功能在4中是使用@Before来达到,相应的tearDonw使用@After,而在所有的Testcase执行之前执行并且仅仅执行一次的方法使用@BeforeClass注解相应的也有@AfterClass

 

测试抛异常的方法:

@Test(expected = Exception.class)

public void myDivide2() throws Exception

{

    cal.divide(1 , 0 );

}

上面的程序表示方法内的语句必须抛出一个Exception异常,该TestCase才会被认为通过,且这时不能再像上面的junit3一样,在程序内把它捕获,捕获后就不再往上抛了,也就会被认为没有通过。

@Test(timeout = 100)

表示TestCase的执行不能超过100毫秒

 

在使用junit4时,使用Assert断言时,eclipse有时会导入junit3的Assert类!!!

 

当某个用例还没有实现完毕或者还不太完善,但又不想影响其它用例的执行,可以使用@Ignore来注解该用例,当执行该类时就不会执行这个用例了

 

运行多个测试类

 @RunWith(Suite.class)

 @SuiteClasses(ATest.class, BTest.class, CTest.class)

 public class ABCSuite {

 }

ABCSuite类里面不用写任何东西都可以

@SuiteClasses这里放的依然是可以为Suite类

 

一个技巧:

可以在每一个包下创建一个suite类,将该包下的所有的测试用例包含进来,然后再有一个总的suite类,它将每一个包下的suite类包含进来,要执行所有的测试用例时就可以只运行这个总的suite类就可以了

 

一个测试用例里不推荐有多个断言,如要测试一个加法方法,不应该把测3+2和4+8放在同一个用例里,但这种问题怎样来解决呢?看下面:

 

参数化测试要点:

 

1. 测试类必须由Parameterized测试运行器修饰

2. 准备数据。数据的准备需要在一个方法中进行,该方法需要满足一定的要求:

 

 1)该方法必须由Parameters注解修饰

 2)该方法必须为public static的

 3)该方法必须返回Collection类型

 4)该方法的名字不做要求

 5)该方法没有参数

  @RunWith(Parameterized.class)

 public class FibonacciTest {

    @Parameters

    public static Collection data() {

          return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },

             { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });

    }

。。

例子

@RunWith(Parameterized.class)

public class ParameterTest

{

    private int expected;

    private int input1;

    private int input2;

 

     public class FibonacciTest {

    @Parameters

    public static Collection data() {

          return Arrays.asList(new Object[][] { {0, 0, 0 }, { 2,1, 1 }, {3, 2, 1 },

             { 5,3, 2 }, {7, 4, 3 }, {10, 5, 5 }, { 14,6, 8 } });

             //数据的构造为0=0+0 , 2 =1+1,3=2+1...

    }

 

    public ParameterTest(int expected , int input1,int input2)

    {

        this.expected = expected;

        this.input1 = input1;

        this.input2 = input2;

    }

 

    @Test

    public void testAdd()

    {

        Calculator cal = new Calculator();

        assertEquals(expected , cal.add(input1,input2));

    }

 

}

 

测试私有方法----用反射

 

 

 int.class == Integer.TYPE int  获取int对应的class对象

 

 Integer.class 获得Integer对应的class对象

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics