jstester-1.3-jdk15.zip

上一篇 / 下一篇  2007-05-17 11:56:24

  • 文件大小: 23 KB
  • 文件版本: V1.0
  • 开发商: 来源网络
  • 文件来源: 本地
  • 界面语言: 简体中文
  • 授权方式: 免费
  • 运行平台: Win9X/Win2000/WinXP

Testing using JsTester

JsTester supports the concept of composition, that is why the main class does not extend JUnit's TestCase. In doing so, it can be used with any version of JUnit, including JUnit4 and annotations, and TestNG since version 1.2. It can also be used inside another TestCase hierarchy.

To use the JsTester features you must follow the following steps:

1. Inline JsTester on your TestCase

You can use JsTester with any class that extends TestCase, usually creating a private variable as the following example:

public class MyTestCase extends TestCase { 
   private JsTester jsTester;

   protected void setUp() throws Exception {
      jsTester jsTester = new JsTester(); 
      // initialize the tester
      jsTester.onSetUp();
   }

   protected void teardown throws Exception {
      // cleanup, don't forget!
      jsTester.onTearDown(); 
   }
}

2. Initialize JsTester with your own validators (optional)

You can insert your own validations any time, just call the loadScript() method and pass the result to eval() before the validator is needed. Tipically all validators can be initialized after onSetUp() was called.

   protected void setUp() throws Exception {
      jsTester jsTester = new JsTester(); 
      // initialize the tester
      jsTester.onSetUp();
      jsTester.eval( jsTester.loadScript("myvalidators.js"));
   }

3. Eval the javaScript. code

Evaling the code will run your javaScript. code in the rhino engine, affecting the global scope as intended (that its what we want to assert). Call eval() as many times as needed.

4. Assert the javaScript. code

JsTester includes the common assert* methods from JUnit (assertNull, assertNotNull, assertEquals, assertTrue) and adds specific checks on javaScript. objects/variables (see javadoc for reference). It also provides the means to execute your own validations, wether they receive one or tow parameters.

Full Example

public class MyTestCase extends TestCase { 
   private JsTester jsTester;

   protected void setUp() throws Exception {
      jsTester jsTester = new JsTester(); 
      // initialize the tester
      jsTester.onSetUp();
      jsTester.eval( jsTester.loadScript("myvalidators.js"));
   }

   protected void tearDown throws Exception {
      // cleanup, don't forget!
      jsTester.onTearDown(); 
   }

   public void testMyJsScript(){ 
      jsTester.eval( jsTester.loadScript("myscript.js") );
      // myobject is a variable created inside myscript.js
      jsTester.assertNotNull("myobject");
   } 

   public void testInlineJs(){ 
      jsTester.eval("var helloFunc = function(){ return 'hello world'; };");
      jsTester.assertIsFunction("helloFunc");
   } 
}

Testing using JsTestCase

When all you want to do is test javaScript. code in isolation, your best option is to extend JsTestCase. This class uses a JsTester internally to test your code.

1. Extend the JsTestCase class

Extend JSTestCase as you would normally do with any TestCase. JsTestCase will take care of the lifecycle of its JsTester.

public class MyTestCase extends JsTestCase { 
   public MyTestCase( String testName ){
      super( testName );
   }
}

2. Initialize your own validators (optional)

You can insert your own validations any time, just call the loadScript() method and pass the result to eval() before the validator is needed. Tipically all validators can be initialized inside the setUp().

   protected void setUp() throws Exception {
      // don't forget to call super.setUp()
      // or the JsTester won't be initialized
      super.setUp();
      eval( loadScript("myvalidators.js") );
   }

3. Eval the javaScript. code

Evaling the code will run your javaScript. code in the rhino engine, affecting the global scope as intended (that its what we want to assert). Call eval() as many times as needed.

4. Assert the javaScript. code

Assert your code as previously explained. The only change is that JsTestCase.assertEquals will not work on your javaScript. code, you must use assertExpressionEquals().

Full Example

public class MyTestCase extends JsTestCase { 
   public MyTestCase( String testName ){
      super( testName );
   }

   protected void setUp() throws Exception {
      // don't forget to call super.setUp()
      // or the JsTester won't be initialized
      super.setUp();
      eval( loadScript("myvalidators.js") );
   }

   public void testMyJsScript(){ 
      eval( loadScript("myscript.js") );
      // myobject is a variable created inside myscript.js
      assertNotNull("myobject");
   } 

   public void testInlineJs(){ 
      eval("var helloFunc = function(){ return 'hello world'; };");
      assertIsFunction("helloFunc");
   } 
}
远程下载

TAG: 单元测试 工具 开源

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2011-06-08  
   1234
567891011
12131415161718
19202122232425
2627282930  

数据统计

  • 访问量: 94405
  • 日志数: 1
  • 影音数: 2
  • 文件数: 192
  • 建立时间: 2007-05-08
  • 更新时间: 2007-08-13

RSS订阅

Open Toolbar