[Test] integrating testcase

Source: Internet
Author: User

In the previous post, I briefly introduced how to write a testcase (mockstrutstestcase is a subclass of testcase). It is very simple to see, basically, you only need to write some testxxx methods to run them. When we choose to run this testcase, we actually run a test, test is the testcase interface, and the testsuite class implements this interface, this class can be used to run multiple testcase together to make it more automated.

It is easier to write a testsuite.CodeYou will understand:

Package edu. PKU. CC. democenter. test;

Import JUnit. Framework. test;
Import JUnit. Framework. testsuite;

Public class alltests {

Public Static Test Suite (){
Testsuite suite =   New Testsuite ( " Test for democenter " );
// $ JUnit-begin $
Suite. addtest (testteacheraction. Suite ());
Suite. addtest (testhibernatedao. Suite ());
// $ JUnit-end $
Return Suite;
}
}

When the testsuite is run, the two testcase instances are automatically tested. As you can see, the testteacheraction class we wrote earlier does not declare the suite method. Yes, so we need to add this static method here, as shown below:

Public Static Test Suite (){
Return   New Testsuite (testteacheraction. Class );
}

In this method, we simply return a testsuite object. JUnit finds and runs all testxxx () methods in this testcase according to the passed parameter (testteacheraction. class.

The preceding Writing of the suite () method is called a dynamic method, that is, the reflection mechanism of Java is used. You can also write static methods, which requires two methods in testcase, as shown below:

Public Static Test Suite (){
Testsuite suite = New Testsuite ();
Suite. addtest ( New Testteacheraction ());
Return Suite;
}

Protected Void Runtest () throws throwable {
Testlistteacheraction ();
Testeditteacheraction ();
Testsaveteacheraction ();
}

This method allows the user to select and execute some testxxx () methods, and these methods do not necessarily start with test, as long as all the specified methods in runtest () are executed. The ratio of the suite () method to the dynamic method also changes. Note that if you write Suite () dynamically, do not overwrite the runtest () method. After my experiment, I found that this will cause runtest () the specified method is repeatedly executed n times, where n is equal to the number of textxxx () methods.

In addition, for junite's test count, in the dynamic mode, JUnit is counted by the number of testxxx (), while in the static mode, it is counted by the number of testcase.

In the dynamic mode, the setup () and teardown () methods are executed before and after each testxxx () method. In the static mode, it is executed before and after each testcase. That is to say, the two test methods in the same testcase may not pass through the teardown () and setup () processes.

For the selection of dynamic and static modes, refer to the above. However, I declare that the above are the conclusions I have obtained through my own tests, and there is a possibility of an error (please be advised), as well as aspects not involved.

I myself prefer the prefer dynamic mode. After all, the code size is smaller.

Related Article

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.