--Junit unit testing using (a)

A, unit testing concepts
Here Insert Picture Description

Uppermost called UI test
called intermediate interface test
lowermost unit testing

一、什么是单元测试

Testing is a code to verify that we can expect to work by means of
the measured object can be any part of our program. Great atmosphere into a single multi-step process, a small tag to function.
Especially the tested object unit test test procedures smallest constituent unit, where the minimum constituent unit may be a function, a class and so on.

二、单元测试的优缺点
Ensure code quality; improve development efficiency

三、测试框架TestNG
1, Description: TestNG is a testing framework for Java, is a very popular practical unit testing framework, a perfect use case management module, with Maven can easily manage third-party plug-ins rely on. TestNG can be done using the functions, interfaces, unit, integration of automated testing. Often combined with selenium do functional automated testing.
2, testers generally use TestNG to write automated tests, developers typically write in Junit unit test
3, TestNG for reasons tester:
       1) ratio Junit covers more versatile testing framework
       2) Junit more suitable for isolating relatively strong test unit
       3) TestNG integration test is more suitable for complex

4, the step of writing a test procedure included:
       1) the preparation and testing of business logic in the code inserted TestNG Annotation
       2) adding to the test information file or in build.xml testng.xml
       3) run TestNG

5, TestNG: Java unit testing framework, powerful than Junit, more detailed level, mainstream

四、测试框架Junit

1 Overview:
       1) was prepared Junit simple reusable frameworks for the test set is a subset of xUnit. xUnit testing framework is based on a set of test-driven development, there PythonUnit, CppUnit, Junit, etc.
       2) Junit test is a programmer tests, the so-called white-box testing, because the programmers know how the software being tested completion and completion of what function
       3) most Java development environments have been integrated as a Junit unit testing tools such as eclipse

2. Why use testing tools
       1) test framework can help us to carry out the purpose programs written tests, help us to avoid possible code bug, in order to ensure the accuracy and stability of the system
       2) a lot of people on their own write code, test simply write main, then syso output console to view the results. This is more boring tedious, is not standardized. Disadvantages: can not run with the test method, the test results to determine the programmer can observe the program logic is correct
       3) Junit assertion mechanism to direct our expected results and the results of running a comparison of the results to ensure predictable sex

3, Junit's Detailed
1) Notes: @ annotation name, as a mark on the back of the content to be interpreted
2) the common name and comment explanation
       @ BeforeEach / @ Before: marking method will be before each test method, is called once
       @Test: marking method as a test method
       @ AfterEach / @ after: marking method will be after each test method is called once
       @ BeforeAll / @ BeforeClass: all methods called once before the start of classes, methods must with a static modification
       @ AfterAll / @ AfterClass: called after the end of all methods of the class time, with a static method must be modified
       @ Disabled / @ ignore: ignore the test method, the test method does not mark the execution
       @DisplayName (String name): set test display name
       @ParameterizedTest: test methods for labeling may be performed by a parametric only annotation flag on @Test
       @MethodSource (value = String name parameter method): a method of parameterization flag, used to test the method provides specific parameter values
参数方法:

public static Collection<Object []> 参数化方法名(){
           return Arrays.asList(new Object[][]  {})}

Asserted: Assert, will achieve the expected results and the results were compared. If they match, the test is successful, otherwise the test fails
Assertions class: the class with all the methods are static modification, can be imported import static way

Description: Class name and comment can not be written the same name; method name can re-name and comment are independent of one error does not affect other methods; location annotation name of the disorder

Published 62 original articles · won praise 42 · views 3310

Guess you like

Origin blog.csdn.net/weixin_45375866/article/details/101646901