C# .NET 单元测试的自动生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/142481/
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
Auto-generation of .NET unit tests
提问by Phobis
Is there such a thing as unit test generation? If so...
有没有单元测试生成这样的东西?如果是这样的话...
...does it work well?
……效果好吗?
...What are the auto generation solutions that are available for .NET?
...可用于 .NET 的自动生成解决方案有哪些?
...are there examples of using a technology like this?
...是否有使用此类技术的示例?
...is this only good for certain types of applications, or could it be used to replace all manually written unit testing?
...这仅适用于某些类型的应用程序,还是可以用来代替所有手动编写的单元测试?
采纳答案by Paul Batum
Take a look at Pex. Its a Microsoft Research project. From the website:
看看Pex。它是一个微软研究项目。从网站:
Pex generates Unit Tests from hand-written Parameterized Unit Tests through Automated Exploratory Testing based on Dynamic Symbolic Execution.
Pex 通过基于动态符号执行的自动探索性测试从手写参数化单元测试生成单元测试。
UPDATE for 2019:
2019 年更新:
As mentioned in the comments, Pex is now called IntelliTestand is a feature of Visual Studio Enterprise Edition. It supports emitting tests in MSTest, MSTest V2, NUnit, and xUnit format and it is extensible so you can use it with other unit test frameworks.
如评论中所述,Pex 现在称为IntelliTest,是 Visual Studio Enterprise Edition 的一项功能。它支持以 MSTest、MSTest V2、NUnit 和 xUnit 格式发出测试,并且它是可扩展的,因此您可以将它与其他单元测试框架一起使用。
But be aware of the following caveats:
但请注意以下注意事项:
- Supports only C# code that targets the .NET Framework.
- Does not support x64 configurations.
- Available in Visual Studio Enterprise Edition only
- 仅支持面向 .NET Framework 的 C# 代码。
- 不支持 x64 配置。
- 仅在 Visual Studio企业版中可用
回答by Jon Limjap
I believe there's no point in Unit test generation, as far as TDD goes.
我相信就 TDD 而言,生成单元测试毫无意义。
You only make unit tests so that you're sure that you (as a developer) are on track w/ regards to design and specs. Once you start generating tests automatically, it loses that purpose. Sure it would probably mean 100% code coverage, but that coverage would be senseless and empty.
您只进行单元测试,以确保您(作为开发人员)在设计和规范方面走在正轨上。一旦你开始自动生成测试,它就失去了这个目的。当然,这可能意味着 100% 的代码覆盖率,但这种覆盖率将是毫无意义和空洞的。
Automated unit tests also mean that your strategy is test-after, which is opposite of TDD's test-before tenet. Again, TDD is not about tests.
自动化单元测试也意味着你的策略是 test-after,这与 TDD 的 test-before 原则相反。同样,TDD 与测试无关。
That being said I believe MSTest does have an automatic unit-test generation tool -- I was able to use one with VS2005.
话虽如此,我相信 MSTest 确实有一个自动单元测试生成工具——我能够在 VS2005 中使用一个。
回答by Hector Sosa Jr
回答by tshak
I agree with Jon. Certain types of testing, like automated fuzz testing, definitely benefit from automated generation. While you can use the facilities of a unit testing framework to accomplish this, this doesn't accomplish the goals associated with good unit test coverage.
我同意乔恩。某些类型的测试,例如自动模糊测试,肯定会从自动生成中受益。虽然您可以使用单元测试框架的功能来实现这一点,但这并不能实现与良好的单元测试覆盖率相关的目标。
回答by S.Lott
I've used tools to generate test cases. I think it works well for higher-level, end-user oriented testing. Stuff that's part of User Acceptance Testing, more so than pure unit testing.
我使用工具来生成测试用例。我认为它适用于更高级别的、面向最终用户的测试。用户验收测试的一部分,比纯单元测试更重要。
I use the unit test tools for this acceptance testing. It works well.
我使用单元测试工具进行此验收测试。它运作良好。
See Tooling to Build Test Cases.
请参阅构建测试用例的工具。
回答by Rikalous
There is a commercial product called AgitarOne (www.agitar.com) that automatically generates JUnit test classes.
I haven't used it so can't comment on how useful it is, but if I was doing a Java project at the moment I would be looking at it.
有一个名为 AgitarOne (www.agitar.com) 的商业产品可以自动生成 JUnit 测试类。
我没有使用过它,所以无法评论它的有用性,但如果我现在正在做一个 Java 项目,我会看看它。
I don't know of a .net equivalent (Agitar did one announce a .net version but AFAIK it never materialised).
我不知道 .net 等价物(Agitar 曾宣布过 .net 版本,但 AFAIK 从未实现)。
回答by qui
回答by zuraff
Parasoft .TESThas a functionality of tests generation. It uses NUnit framework for tests description and assertions evaluation.
Parasoft .TEST具有测试生成功能。它使用 NUnit 框架进行测试描述和断言评估。
It is possible to prepare a regression tests suite by automated generating scenarios (constructing inputs and calling tested method) and creating assertions which are based on the current code base behavior. Later, after code base under tests evolves, assertions indicates regressions or can be easily recorded again.
可以通过自动生成场景(构建输入和调用测试方法)并创建基于当前代码库行为的断言来准备回归测试套件。稍后,在被测代码库演变之后,断言表示回归或可以轻松地再次记录。
回答by Bouam
I know this thread is old but for the sake of all developpers, there is a good library called unit test generator:
我知道这个线程很旧,但为了所有开发人员,有一个很好的库,称为单元测试生成器:
https://visualstudiogallery.msdn.microsoft.com/45208924-e7b0-45df-8cff-165b505a38d7
https://visualstudiogallery.msdn.microsoft.com/45208924-e7b0-45df-8cff-165b505a38d7
Good dev
好开发
回答by Benjamin Craig Adams
GennyMcGenFacecreates a unit test for each function in your class and generates objects with random words/values in each parameter.
GennyMcGenFace为类中的每个函数创建一个单元测试,并在每个参数中生成具有随机单词/值的对象。
- Generate unit tests for each function in your class
- Figures out valid randomly generated values for the paramater inputs and the returns statement.
- Mockable interfaces return valid randomly generated values
- Generate unit tests for each function in your class
- Imports all the needed namespaces into your test class
- 为类中的每个函数生成单元测试
- 为参数输入和返回语句找出有效的随机生成的值。
- Mockable 接口返回有效的随机生成的值
- 为类中的每个函数生成单元测试
- 将所有需要的命名空间导入您的测试类
It helps in setting up your unit tests, especially if you have input objects with lots of parameters.
它有助于设置您的单元测试,尤其是当您有包含大量参数的输入对象时。
The unit test will look something like this
单元测试看起来像这样