简体   繁体   English

如何对不透明代码进行单元测试?

[英]How to unit test opaque code?

I'm using some old C# code (specifically this Fortune's Voronoi graph algorithm ) in a Unity3D project and I wanted to update it to use proper generics, refactor and generally clean things up. 我在Unity3D项目中使用了一些旧的C#代码(特别是Fortune的Voronoi图算法 ),我想对其进行更新以使用适当的泛型,重构并通常进行清理。

Ideally, I'd do this without breaking anything; 理想情况下,我会做到这一点而不会破坏任何内容。 the code works and its implementation of the algorithm is sound. 代码有效,算法的实现是合理的。 Unit tests would obviously help me refactor this without screwing it up. 单元测试显然可以帮助我进行重构而不用搞砸。

Unfortunately I really don't understand the math or the algorithm, and the code is dense and comment-free. 不幸的是,我真的不懂数学或算法,并且代码很密集且没有注释。

How can I write unit tests for code like this? 如何为这样的代码编写单元测试?

Unit testing is all about input and output of methods. 单元测试全部与方法的输入和输出有关。

So you could just single out methods, execute them with several sets of parameters, and store the result. 因此,您可以仅选择方法,使用几组参数执行它们,然后存储结果。

Then in your Unit Tests, you execute the same methods, with the same sets of parameters, and you know what to expect as output. 然后在单元测试中,使用相同的参数集执行相同的方法,并且知道期望输出什么。 If the output changes, you broke something. 如果输出发生变化,则说明发生了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM