Junit(一)

一、一手鞋

  官网地址:http://junit.org/

二、搭建过程

  较简单,略

三、Getting Started

  Eclipse中可直接引入JUnit4包使用

四、使用技巧

      (一) Assertions 断言

   1. 可重载(Overload),第一个参数通常为错误信息

       2. assertThat于assert不同,错误信息可选,需要Matcher参数

     3. 示例

    (1) assertArrayEquals

                  byte[] expected = "trial".getBytes();

                  byte[] actual = "trial".getBytes();

                  org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);

           (2)testAssertEquals

                   org.junit.Assert.assertEquals("failure - strings are not equal", "text", "text");

           (3)testAssertFalse

                   org.junit.Assert.assertFalse("failure - should be false", false);

           (4)testAssertNotNull()

                  org.junit.Assert.assertNotNull("should not be null", new Object());

      (二) Test runners

    1. NetBeans, Eclipse and IntelliJ Idea have native graphical test runners built in.

    2. 示例

      public static Test suite() { return new JUnit4TestAdapter('YourJUnit4TestClass'.class); }

       (三)Aggregating tests in suites

    1. 集中测试

    2. 示例

     import org.junit.runner.RunWith;

               import org.junit.runners.Suite;

               @RunWith(Suite.class)

               @Suite.SuiteClasses({

                       TestFeatureLogin.class,

                       TestFeatureLogout.class,

                       TestFeatureNavigate.class,

                       TestFeatureUpdate.class })

                 public class FeatureTestSuite {

                       // the class remains empty,

                       // used only as a holder for the above annotations

                  }

  (四)执行顺序

   1. @FixMethodOrder(MethodSorters.JVM)

         2. @FixMethodOrder(MethodSorters.NAME_ASCENDING),按照名称排序

posted on 2015-11-11 21:30  商容  阅读(187)  评论(0)    收藏  举报

导航