C# 自动生成 .NET 和 Java 的单元测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/689087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatic generation of Unit test cases for .NET and Java
提问by yoitsfrancis
Is there a good tool to generate unit test cases given say a .NET or Java project, it generates unit test cases that would cover an almost 100% code coverage. The number of test cases could be directly proportional to the cyclomatic complexity of the code (the higher the nesting of loops and conditions the higher the cyclomatic complexity) where the higher the cyclomatic complexity, the greater the set of test cases are generated. I'm not expecting it to be fully functional (say I'm going to build the unit tests and run it after its been generated), but I would say that it can have a template style in the test case where you are to modify the case that suits your intended needs. But it should also have a proper setup and teardown method and is good enough to detect if mock objects for unit testing should be used should there be any dependencies. So, is there such a tool that exists?
给定一个 .NET 或 Java 项目,是否有一个很好的工具来生成单元测试用例,它生成的单元测试用例几乎可以覆盖 100% 的代码覆盖率。测试用例的数量可以与代码的圈复杂度成正比(循环和条件的嵌套越高,圈复杂度越高),其中圈复杂度越高,生成的测试用例集就越大。我不希望它具有完整的功能(假设我将构建单元测试并在生成后运行它),但我想说它可以在您要修改的测试用例中具有模板样式适合您的预期需求的情况。但它也应该有一个适当的设置和拆卸方法,并且足以检测是否应该使用单元测试的模拟对象,如果有任何依赖。那么,是否存在这样的工具?
采纳答案by Jon Skeet
For .NET, Microsoft has Pexwhich will hopefully go mainstream for .NET 4.0, along with Code Contracts. I highly recommend watching the Channel 9 video.
对于 .NET,Microsoft 拥有Pex,它有望与Code Contracts一起成为 .NET 4.0 的主流。我强烈建议您观看第 9 频道的视频。
It strikes me that this sort of thing is very good for very data-driven classes - parsers etc. I can't see that I'd very often start offwith it, but a useful tool to have in your armoury nonetheless.
令我震惊的是,这种东西对于非常数据驱动的类 - 解析器等非常有用。我看不出我经常从它开始,但仍然是您的军械库中的有用工具。
回答by Marc Gravell
回答by Kurt Schelfthout
Although it seems counter-intuituve, you may also be interested in random test generation frameworks. Research has proven that it can be just as effective in finding bugs than systematic approaches based on coverage, as you suggest.
尽管这似乎违反直觉,但您可能也对随机测试生成框架感兴趣。研究已经证明,正如您所建议的那样,它在查找错误方面与基于覆盖率的系统方法一样有效。
Check out Randoopboth for .NET and Java. It works by generating a more or less random sequence of method calls, and checks contracts, crashes etc. It is fully automatic.
查看适用于 .NET 和 Java 的Randoop。它通过生成或多或少随机的方法调用序列来工作,并检查合同、崩溃等。它是全自动的。
Also you may want to check out some other random testing tools based on QuickCheck, e.g. for Java, Scala, F#. that are more similar to Pex, i.e. you give a specification, or parametrized unit test, and the tool checks it for a number of generated input arguments.
此外,您可能还想查看其他一些基于QuickCheck 的随机测试工具,例如 Java、Scala、F#。更类似于 Pex,即您给出一个规范或参数化单元测试,并且该工具会检查它是否有许多生成的输入参数。
I've found that this "parametrized" way of writing unit tests is actually a lot more natural in at least 60% of the cases, and finds lots more bugs.
我发现这种编写单元测试的“参数化”方式实际上在至少 60% 的情况下更自然,并且发现了更多错误。
回答by arcuri82
回答by saurabheights
For Java, try JUnit-Tools. It has own eclipse plugin along with good documentation.
对于 Java,请尝试JUnit-Tools。它有自己的 eclipse 插件以及良好的文档。