📎 Webclip
More Testing Tips With C#
The post gathers a few C# testing tips around test data, reference outputs, and integration-test setup. It says test data generators help when hand-written data gets tedious, seeds make randomized tests reproducible, and reference files work well for stable rendering output.
It also shows how to find a free TCP port with Socket, and notes that the whole 127.0.0.0/8 loopback range can be used when a fixed port is required.
Reading notes#
- In tests, data generators help when creating examples by hand becomes tedious and the data ends up unrealistic.
- The text presents
Fakeras an option that produces data in English and more “human” data, without focusing on edge cases. Bogus.NETis shown as a broader library, with test object factories, support for multiple locales, and additional data sets.- When using random data in tests, the text recommends controlling the seed so that a failure can be reproduced with the same data set.
- The seed can be set globally in
Bogus.Randomizer.Seedor per instance withUseSeed. - This style of testing is described as a simple form of property-based testing, because it generates random data and checks whether a system property holds.
- Another technique is to save a good reference of the result and compare it with the current output, which works well for rendering code such as HTML, images, or SVG.
- When the test fails in this model, the text suggests inspecting the differences and, if the change is expected, copying the files from
currenttoreferenceand committing. - For integration tests, the text shows how to discover a free TCP port using
Socketbound to127.0.0.1:0. - If a fixed port is required, the text says the
127.0.0.0/8range belongs to the local machine and allows using many loopback IPs, such as127.0.0.2and127.0.0.3, on the same port.
