TDD and unit testing

1. TDD development process

        Looking back at the development process of TDD, we are constantly repeating the following process until the requirements are completed.


2. The benefits of TDD


3. Unit testing

        TDD is test-driven development, which is of course closely related to unit testing.

1. The purpose of unit testing:
a. Embrace change
b. Ensure quality
c. Good unit testing can be used as documentation
d. It is also an important design tool

2. Unit testing process : 3A

3. Test class
a. Naming: [ Tested class]Test
b. The test class and the tested class are under the same package name
c. One tested class corresponds to one test class

4. Test purpose and intent:
a. Express the test purpose and intent through the method name
b.Given- When-Then

5. Test methods:
a. The case must have verification
b. A test case only tests one scenario
c. The cases are independent and do not depend on each other
d. Do not try/catch exceptions in the test plan
e. Do not appear System in the test method. out or Logger

6. Test case trilogy : Arrange-Act-Assert

7. Why write a single test:
a. Fast feedback
b. Confidence
c. Progress visualization
d. Clear goals
e. Focus
f. Instructions
g. Regression testing
h .split function
i.testable
j.quality assurance
k.design

8. Junit Cheat Sheet
a. Annotations
@Test b. Assertions assertEquals assertTrue/assertFalse assertNull/assertNotNull
assertSame /assertNotSame: compare reference fail/pass 9. Unit test Adv.Assertion a. Constraint validation: assertThat([actual],[matcher] ) is(true) not(nullValue()) either(containsString("a")).or(containsString("b")) anyOf(containsString("a"),containsString("b")) hasItem(orderl) b. Third party/custom assertion c. Prepare for each test case: @Before clean up: @After d. Prepare for each test class: BeforeClass clean up: AfterClass






















10.单元测试样例
public class StringParserTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public voi should_throw_exception_when_given_character() {
   expectedException.expect(IllegalArgumentException.class);
   expectedException.expectMessage("Argument should be int");
  
   new StringParser().parseAndSum("a");
}

@Test(expected = IllegalArgumentException.class)
public void should_throw_exception_when_given_null() {
   new StringParser().parseAndSum(null);  
}
}

Timeout
public class SquareTest {
    @Test(timeout = 1000)
    public void squareRoot() {
        calculator.squareRoot(4);
        assertEquals(2, claculator.getResult());
    }
}

参数化运行Junit
@RunWith(Parameterized.class)
public class EmailUtilsParameterizedTest {
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
   
        return Arrays.asList


@RunWith(Theories.class)
public class EmailUtilsTheoryTest {
@DataPoints
public static TestData[] ARRAYS = new

@Theory

11.Test Data
a.Data Mother
b.Test data organization Builder

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326458739&siteId=291194637