Small concept
Unit testing: Tests functions and classes. Unit test coverage: A measure of software testing. The percentage of functional code that has been unit-tested. There are a number of automated test framework tools that can provide this statistic, the most basic of which is: Unit test coverage = lines of code under test/total function of reference code *100%
Originally, unit tests had to be developed and written. How to write?
To write a standard professional-looking unit test, the code structure has these parts: prepare, invoke, assert
Preparation: Prepares the external environment to call (the unit under test), such as data, stubs, mocks, temporary variables, call requests, context variables, etc. : is the method under test (function, process itself) assertion: The assertion part determines whether the result returned by the calling part is as expected
So how do you get to those three important parts?
- Prepare – driver: you write a main function to call the method you are testing. Pile program: used to simulate the work process of the module under test called by the lower module, that is, the module under test itself called other associated functions, pile refers to used to replace the associated code or unrealized code, in order to test the object can be normally executed. Typically, some input and output are hardcoded.
- Call – Write the method to be tested
- Assertions, Mock. Mocks not only guarantee the functions of stubs, but also simulate the interaction between objects in depth, such as how many times they are called, whether exceptions will be thrown in a certain situation, and providing data assertions.
The above drivers, stubs, and mocks are all descriptions of things without any technology or tools. In written unit test code, code that meets these criteria is called “driver, stub, Mock.” Just to make it clear and easy to use.
Automatic test
Of course, writing unit test code by hand would be crazy, too much. Tools are available: JUnit, PHPUnit, Codeception, Behat, PHPSpec, SimpleTest, Storyplayer, Peridot, Atoum, Kahlan, Selenium. This is where automated testing comes in.
Some misconceptions
In previous tests, the URL was accessed through the browser. Normally, a unit test case should command the test script.