New approach to unit testing – Sohu Technology

A new approach to unit testing – why do we write unit tests for Sohu? " to meet the needs of " is a prerequisite for the existence of all software, unit testing must serve it. From this point of view, we can sum up the two motivations of writing unit tests: drivers (e.g. TDD) and verification function implementation. In addition, the characteristics of the "Volatile" software requirements determine the necessity of modifying the code. In this case, the unit test can protect the existing functions from being destroyed. Based on the above two points, we look at the characteristics of traditional unit testing? Based on the use case test (By Example) unit test the most common routine is Given, When, Then trilogy. When writing, we will carefully prepare (Given) a set of input data, and then after the call behavior, the results of the assertion is consistent with expectations. This use case based test method in the development (including TDD) process is very easy to use. Because it clearly defines the input and output, and in most cases the amount is very small, easy to understand. But there’s a downside to this approach. To assist the unit test to improve these two points. I am here to introduce another test method – generative test (Generative Testing, also known as Property-Based Testing). This test is based on the input hypothesis output and generates many possible data to verify the validity of the assumptions. Generative test for the first question, we change the thinking. If we do not write specific test cases, but directly describe the intention, then the problem will be smoothly done or easily solved. The idea is good, but how to practice Given, When, Then? The answer is to allow the program to automatically generate references and verify the results. This leads to the concept of "generative test" – we first declare the possible situation of incoming data, and then use the generator to generate the data that is in accordance with the reference data. Given Clojure (Alpha) 1.9 new built-in Clojure.spec can easily do this:; possible input parameters; definition: two integer parameters (s: def: add-operators (s cat: a int: B? Int?));; try to generate data (Gen generate (s: Gen: add-operators));; data generated by -> (1 -122); first, we try to declare two parameters may be the case or called specifications (specification), namely a and B are integer parameter. The generator then generates a pair of integers. The whole process analysis and construction, are not related to specific data, it will force us to try to figure out the appearance of input data, but also can avoid the test s相关的主题文章: