JUnit by Erich Gamma and Kent Beck prepared a regression testing framework (regression testing framework). Junit test is the programmer tests, the so-called white-box testing, because the programmers know how the software being tested (How) completion and the completion of what (What) function. Junit is a framework that succession TestCase class, you can use the automatic Junit test.
directory
junit junit advantage of a feature, for xp programming, 2, 3, for reconstruction purposes, the above two cases for non-installation of JUnit test using JUnit to write junit JUnit best practices characteristics
junit
JUnit is an open source Java testing framework for writing and running repeatable tests. He is a system of xUnit unit testing framework for an instance (for java language). It includes the following features: 1, for testing expected results of the assertion (Assertion) 2, for sharing common test data test tools 3, for the convenience of the organization and operation of the test test suite 4, graphics and text test runner Another advantage junit
junit
External junit programming and reconstruction in xp (refactor) was strongly recommended tool,
nike air force 1, as automated unit testing in the case of the development can greatly increase efficiency, but the actual is to write test code takes a lot of time and effort, then the benefits of using this stuff Where is it? I believe that is this: 1, for the xp required programming, writing tests before writing code, which can force you to think to write good code before the code (methods) of the function and logic, or code written in a very unstable then you need while maintaining test code and the actual code, this workload will increase significantly. Therefore, in xp programming,
air force 1 shoes, the basic process is this: ideas -> write test code -> code -> test, and writing tests and writing code are incremental, and write a little test that in the preparation of the future If you find problems in the code block can be tracked over the problem, reduce the regression error correction more difficult. 2, for the reconstruction in terms of its benefits and xp programming is similar, because the reconstruction is also required to change a little test, less time consuming due to return errors. 3, for more than two cases of non-
junit
When we use in the development of appropriate test junit write is necessary, because the general test we also need to write code,
air force one high, you may not use the original junit If using junit, but also against the interface (methods) to prepare test code will reduce future maintenance work, such as the future of the method of internal changes (this is equivalent to reconstruction work.) Junit assertion because there is another function, if the test results do not pass the test will tell us that do not pass, why, and if the previous general practice is to write some test code is to look at their output, and then to determine by themselves the results of using the correct using junit advantage is that this result is correct to judge it to complete, we only need to look at it the right to tell us whether the results can, in general, will greatly improve efficiency. Installing JUnit An
junit
loading is very simple, first download the latest the following address zip package: extract the downloaded later to your favorite directory, the assumption is JUNIT_HOME, then JUNIT_HOME junit.jar under pack added to your system CLASSPATH environment variable, for the IDE environment, the need for increased use of junit lib in the project, which set different IDE have different settings, where not much to say. How to use JUnit to write tests
Practice Software Testing and junit
The simplest example is as follows: 1, create a subclass of TestCase package junitfaq; import java.util .*; import junit.framework .*; public class SimpleTest extends TestCase {public SimpleTest (String name) {super (name);} 2, write a test method assert the expected results public void testEmptyCollection () {Collection collection = new ArrayList (); assertTrue (collection.isEmpty ()); } Note
junit
Italian: JUnit test approach is recommended as the beginning of the method to be tested, so that these methods can be automatically found by testing. 3,
nike air force one, write a suite () method, which uses reflection to dynamically create a method that contains all of the testXxxx test suite public static Test suite () {return new TestSuite (SimpleTest.class);} 4, write a main () method text runner convenient way to run the test public static void main (String args []) {junit.textui.TestRunner.run (suite ());}} 5,
air force one shoes, run the test run in text mode: java junitfaq.SimpleTest by The test results are:. Time: 0 OK (1 tests) Time tested on a small number of points that, if testing shows OK. Otherwise, the small point behind the marked F, indicates that the test failed. Each of the test results should be OK, so in order to explain the test is successful, if unsuccessful the information will immediately follow the prompts revised. JUnit test report if not successful, it will distinguish between failures (failures) and mistakes (errors). Failure is the code in your method fails due to assert; and the error is caused by abnormal code, such as ArrayIndexOutOfBound######ception. Graphically run: java junit.swingui.TestRunner junitfaq.SimpleTest the test results through the graphical interface of the green bar in part. These are the most simple test sample, the actual test,
nike air force one high, we test the function of a class is often necessary to perform some common operations, after the completion of the destruction of the resources needed (such as network connections, database connections, close open files etc.), TestCase class provides us with a setUp method and tearDown methods, setUp method to test your content in the preparation of the TestCase subclass method before each testXxxx will run, and tearDown methods of content after the end of each method will be executed testXxxx . The shared initialization code only, but also eliminates the potential between each test code interaction. JUnit best practices, Martin Fowler said: and the test program seemed to slow you down. Soon after, however, you will find that you repeat the same Fixture, and the new test is usually only involves adding a new test method. You may write a lot of test code, but you soon find that you envisage only a small part of the test is really useful. You need to test is the test that will fail, that you think will not fail those tests, or do you think they should fail the test of success. As we mentioned earlier testing is a process without interruption. Once you have a test, you should ensure that their work has been to test your work by adding new code. Do not run every few days or the last test day, you should run this test code. This investment is very small, but it can work to ensure that you get reliable code. Reduce your rework rate, you will have more time to write working code. Do not think that pressure, do not write test code. Instead write test code will gradually reduce your stress, because by writing test code
junit
, the behavior of your class have the exact understanding. You will be faster to write code to work efficiently. Here are some specific skills in writing test code or good practices: 1. Do not use the TestCase constructor initializes Fixture, but rather use the setUp () and tearDown () method. 2. Do not rely on or assume that the order of test runs, because the JUnit test method using Vector saved. So different platforms will be in a different order, remove from the Vector test methods. 3. To avoid the side effects of the preparation of a TestCase. For example: If subsequent tests depends on the specific transaction data, do not submit transaction data. Simple rollback on it. 4. When inheriting a test class, remember to call the superclass setUp () and tearDown () method. 5. Will test the code and working code together, while simultaneously compile and update. (Using the junit Ant had to support the task.) 6. Test classes and test methods should have a consistent naming scheme. If the working class name with the test to form the test class name. 7. To ensure that the test is independent of time, do not rely on outdated data used for testing. Resulted in the subsequent maintenance of the process is difficult to reproduce the test. 8. If you are writing software for the international market, the preparation of the test to consider when international factors. Native Locale not only for testing. 9. As much as possible to provide the use of JUnit to assert / fail method and the method of exception handling can make the code more concise. 10. The test to be as small as possible, the implementation of speed. 11. Do not mandatory data file path. 12. Using auto-Junit testing exception handling code is written in simple to use Junit in fact try-catch to catch the exception is not necessary, Junit will automatically catch the exception. Those who are not exceptions to be caught as error handling. 13. Full use Junit to assert / fail method assertSame () to test whether two references point to the same object assertEquals () to test whether two objects are equal to 14. To ensure the test code has nothing to do with the time 15. Do use the document generator test document. JUnit and ant ant provides a combination of the two target: junit and junitreport run all test cases, and generate html-formatted report the specific operation is as follows: 1. The junit.jar on ANT_HOMElib directory 2. Modify build.xml, add the following : -------------- One or more tests failed, check the report for detail ... --------------------- -------- run this target, ant will run each TestCase, the report directory will have a lot of TEST *. xml and some open the report directory web page you can see a very intuitive index.html test run reports at a glance. Developed in Eclipse, run the JUnit test is fairly simple. JUnit integration for Eclipse itself, related components, and provides a seamless operation of JUnit support. Atlas Atlas entry for more extended reading: 1
http://www.junit.org JUnit web site can be downloaded from the site JUnit, and find information
2
http:/ / java.exampleshow.com / sourcecode / Category: JUnit JUnit example
3
http://www.eclipse.org leading Java integrated development environment. Provides good support for JUnit
4
http://www.ithao123.com/beginner/junit.html
Open Category: programming, computers, software testing, regression testing, I have to perfect junit