Writing JUnit Test Case Generation Using codepro

Source: Internet
Author: User

 

 

Google recently released a powerful testing tool codepro, which can be installed as an Eclipse plug-in. It has tested the testcase generation function and is very powerful.

Program to be tested: Test. Java

Public class test {<br/> Public int methodtotest (int A, Boolean cond1, Boolean cond2, Boolean cond3) <br/>{< br/> If (cond1) <br/>{< br/> A ++; <br/>}< br/> If (cond2) <br/>{< br/> --; <br/>}< br/> If (cond3) <br/>{< br/> A = A; <br/>}< br/> return; <br/>}< br/>}

Automatically generate a testtest Project, which contains two classes that can run independently: testtest. Java & testall. Java

Import JUnit. framework. *; <br/>/** <br/> * The class <code> testtest </code> contains tests for the class <code >{@ link test} </code>. <br/> * @ generatedby codepro at 12/23/10 PM <br/> * @ author Andrew <br/> * @ version $ Revision: 1.0 $ <br/> */<br/> public class testtest extends testcase {<br/>/** <br/> * run the int methodtotest (INT, Boolean, boolean, Boolean) method test. <br/> * @ throws exception <br/> * @ generatedby codepro at 12/23/10 PM <br/> */<br /> Public void testmethodtotest_1 () <br/> throws exception {<br/> test fixture = new test (); <br/> int A = 1; <br/> Boolean cond1 = false; <br/> Boolean cond2 = true; <br/> Boolean cond3 = true; <br/> int result = fixture. methodtotest (A, cond1, cond2, cond3); <br/> // Add additional test code here <br/> assertequals (0, result ); <br/>}< br/>/** <br/> * run the int methodtotest (INT, Boolean) method test. <br/> * @ throws exception <br/> * @ generatedby codepro at 12/23/10 PM <br/> */<br /> Public void testmethodtotest_2 () <br/> throws exception {<br/> test fixture = new test (); <br/> int A = 1; <br/> Boolean cond1 = true; <br/> Boolean cond2 = false; <br/> Boolean cond3 = false; <br/> int result = fixture. methodtotest (A, cond1, cond2, cond3); <br/> // Add additional test code here <br/> assertequals (2, result ); <br/>}< br/>/** <br/> * perform pre-test initialization. <br/> * @ throws exception <br/> * If the initialization fails for some reason <br/> * @ see testcase # setup () <br/> * @ generatedby codepro at 12/23/10 PM <br/> */<br/> protected void setup () <br/> throws exception {<br/> super. setup (); <br/> // Add additional set up code here <br/>}< br/>/** <br/> * perform post-test clean-up. <br/> * @ throws exception <br/> * If the clean-up fails for some reason <br/> * @ see testcase # teardown () <br/> * @ generatedby codepro at 12/23/10 PM <br/> */<br/> protected void teardown () <br/> throws exception {<br/> super. teardown (); <br/> // Add additional tear down code here <br/>}< br/>/** <br/> * launch the test. <br/> * @ Param ARGs the command line arguments <br/> * @ generatedby codepro at 12/23/10 PM <br/> */<br/> Public static void main (string [] ARGs) {<br/> If (ARGs. length = 0) {<br/> // run all of the tests <br/> JUnit. textui. testrunner. run (testtest. class); <br/>}else {<br/> // run only the named tests <br/> testsuite suite = new testsuite ("selected tests "); <br/> for (INT I = 0; I <args. length; I ++) {<br/> testcase test = new testtest (); <br/> test. setname (ARGs [I]); <br/> suite. addtest (TEST); <br/>}< br/> JUnit. textui. testrunner. run (suite); <br/>}< br/>}
 

 

Import JUnit. framework. test; <br/> Import JUnit. framework. testsuite; <br/>/** <br/> * The class <code> testall </code> builds a suite that can be used to run all <br/> * of the tests within its package as well as within any subpackages of its <br/> * package. <br/> * @ generatedby codepro at 12/23/10 PM <br/> * @ author Andrew <br/> * @ version $ Revision: 1.0 $ <br/> */<br/> public class testall {<br/>/** <br/> * launch the test. <br/> * @ Param ARGs the command line arguments <br/> * @ generatedby codepro at 12/23/10 PM <br/> */<br/> Public static void main (string [] ARGs) {<br/> JUnit. textui. testrunner. run (Suite ()); <br/>}< br/>/** <br/> * Create a test suite that can run all of the test cases in this package <br/> * And all subpackages. <br/> * @ return the test suite that was created <br/> * @ generatedby codepro at 12/23/10 PM <br/> */<br/> Public Static Test Suite () {<br/> testsuite suite; </P> <p> suite = new testsuite ("tests in package"); <br/> suite. addtestsuite (testtest. class); <br/> return suite; <br/>}< br/>}

 

Testtest. Java allows you to specify test case by passing parameters to the main function, while testall. Java runs all test cases.

Both testtest. Java and testall. Java can be run as a JUnit app or Java app.

 

Two tips

  • Passing parameters to the main function in eclipse

Run> RUN Configuration

Find the application to modify parameters, such as testtest

> (X) Arguments

The content entered in program arguments will be passed to the main function.

VM arguments passed to Java Virtual Machine

  • Enable assert in eclipse

VM arguments:-Ea

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.