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
国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97