Junit unit testing concepts (test language Java)
Others
2020-02-16 10:44:20
views: null
A unit testing concepts
1. What is unit testing
- Unit testing is to write test for the smallest functional unit code
- Java program smallest functional unit approach
- Unit testing is to test for a single Java method
2. Use disadvantage main () Test Method
- Only one main () method, the test code can not be separated
- No print out the test results and expected results
such as: expected: 63533, but actual: 89653
- So then you need a testing framework to help us write a test
3. The benefits of unit testing
- A single method to ensure normal operation
- If a method of modifying the code only needs to be ensured by the corresponding unit test
- Code itself can be used as test sample code
- All tests can be automated and access to report
4. JUnit Introduction
- JUnit is an open source unit testing framework for the Java language
- Designed specifically for the Java language, the most widely used
- JUnit is the de facto standard unit testing framework
4. JUnit Features
- Use the assertion (Assertion) test results expected
- It can easily organize and run tests
- You can easily view the test results
- Common IDE (e.g. Eclipse) are integrated JUnit
- It can be easily integrated into Maven
5. JUnit design
- TestCase: a TestCase represents a test
- TestSuite: a TestSuite contains a set of TestCase, represents a group of test
- TestFixture: a TestFixture represents a test environment
- TestResult: for the collection of test results
- TestRunner: used to run tests
- TestListener: used to monitor the testing process, to collect test data
- Assert: to assert the test results are correct
6. Assert assertion
- Assert equal: assertEquals (expected value, the test results)
- Assertion arrays are equal: assertArrayEquals ((1,2,3), x)
- Float assert equal: assertEquals (3.1412, X, 0.00001)
- Assertion is null: assertNull (x)
- Asserted true / false: assertTrue (x> 0) assertFalse (x <0)
- Other: assertNotEquals / assertNotNull
7. Junit unit test point to note
- A TestCase contains a set of related test methods
- Assert assertion using the test results (note float assertEquals to specify delta)
- Each test method must be totally independent
- It must be very simple test code
- You can not write tests for testing code
- Testing needs to cover a variety of input conditions, especially boundary conditions
Published 33 original articles
·
won praise 11
·
views 10000 +
Origin blog.csdn.net/lsy_666/article/details/104326072