zoukankan      html  css  js  c++  java
  • junit测试范例

     public class HelloWorld
     {
        public String say()
      {
     return "Hello World";
      }
     }

    //------------------------------------
    import junit.framework.*;
         public class TestHelloWorld extends TestCase {
         private HelloWorld myHelloWorld;
         private String expectedStrings;
         protected void setUp() {
           expectedStrings = ”Hello World”;
           myHelloWorld = new HelloWorld();
       }
        public static Test suite() {
            return new TestSuite(TestHelloWorld.class); //一组测试用例的集合
       }
         public void testSay() {
            assertEquals(expectedStrings, myHelloWorld.say());
           /*在junit.framework.Assert类中定义了相当多的assert方法*/
       }
     }


    //--------------------------------------
    测试类的编写
      1. 导入Junit类(import语句)
      2. 生成TestCase类的子类
      3. 添加需要使用的实例变量(Fixture)
      4. 覆盖setUp()方法初始化实例变量(Fixture)
      5. 静态方法suit()用来执行测试 ,通过将测试实例添
          加到TestSuite对象中实现运行多个测试
      6. 定义一系列无返回值的公共方法testXXX()
      7. 覆盖tearDown()方法释放setUp()方法中建立的永久  
          资源
      


    //-----------------------------------------------
    运行测试类
        java junit.textui.TestRunner TestHelloWorld
       
        TestRunner是测试运行器,JUnit提供了三种界面来运行测试 : [Text  UI] junit.textui.TestRunner [AWT   UI] junit.awtui.TestRunner [Swing UI] junit.swingui.TestRunner   
       也可在测试类中定义如下主函数代替suite()方法执行测试:
        public static void main(String args[])
      { junit.textui.TestRunner.run(TestHelloWorld.class);
      }
        此时只需运行:java TestHelloWorld
       


    //------------------------------------------------

  • 相关阅读:
    BZOJ3512 DZY Loves Math IV
    HDU5608 function
    数论
    动态点分治
    tarjan
    插头DP
    斯坦纳树
    css中的display(显示)和visibility(可见性)
    简单的数据整理, 递归算法
    java链接数据库构建sql语句的时候容易记混的地方
  • 原文地址:https://www.cnblogs.com/zhouwenwu/p/2280773.html
Copyright © 2011-2022 走看看