How do you write tests for code that requires duplication of test code?

I am working to bring as much logic out of the user control as possible so that it can be tested on the module in order to reduce the burden of manual testing. I am having problems with situations where the test method creates a complex result; By writing a test case that computes the result, it will include a record of what is essentially the test code in the test itself.

For example, I have a class GeometryGeneratorthat creates WPF geometry based on class properties. In one configuration is generated PathGeometry, consisting of ArcSegment. I can calculate which arc properties should be based on test parameters, but this calculation is identical to the code I'm trying to verify. This seems to make the test ineffective; if there is an error in the calculation, there will be an error in the test, and if changes as a result of a change in the method can change in the test.

What should I do with this situation? The only approach I came up with is to manually calculate the results of my test cases and hard-code these values ​​in the tests. Is this an acceptable approach (it seems that I would do if I wrote tests before implementation)?

+3
source share
4 answers

The only approach I came up with is to manually calculate the results of my test cases and hard-code these values ​​in the tests. Is this an acceptable approach (it seems that I would do if I wrote tests before implementation)?

Yes, this is typical. For unit testing purposes, you must assume that the formula you use to calculate your results is correct. This is the software implementation you are testing.

, , , ( ). , , , .

+4

, .

"" . , , , , .

+1

, , , ,

const int expectedResult = 2 * 4/someConstant;//, .

, , .

+1

A "unit test" . GeometryGenerator, PathGeometry, ArcSegment ..

, , , ( GeometryGenerator, ArcSegment 1, vs, , 0 ..).

If your unit test starts to look like you are duplicating existing code to test certain functionality, I'm afraid that you are doing unit testing incorrectly and that there is a simpler and more efficient solution for you.

0
source

Source: https://habr.com/ru/post/1709534/


All Articles