Auto-generation of .NET unit tests

C#.NetUnit TestingAutomationAutomated Tests

C# Problem Overview


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?

...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?

C# Solutions


Solution 1 - C#

Take a look at Pex. Its a Microsoft Research project. From the website:

Pex generates Unit Tests from hand-written Parameterized Unit Tests through Automated Exploratory Testing based on Dynamic Symbolic Execution.

UPDATE for 2019:

As mentioned in the comments, Pex is now called IntelliTest and 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.

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

Solution 2 - C#

I believe there's no point in Unit test generation, as far as TDD goes.

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.

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.

That being said I believe MSTest does have an automatic unit-test generation tool -- I was able to use one with VS2005.

Solution 3 - C#

Updated for 2017:

Unit Test Boilerplate Generator works for VS 2015-2017 and is being maintained. Seems to work as advertised.

Solution 4 - C#

I created ErrorUnit. It generates MSTest or NUnit unit tests from your paused Visual Studio or your error logs; Mocking class variables, Method Parameters, and EF Data access so far. See http://ErrorUnit.com

No Unit Test generator can do everything. Unit Tests are classically separated into three parts Arrange, Act, and Assert; the Arrange portion is the largest part of a unit test, and it sets up all the preconditions to a test, mocking all the data that is going to be acted upon in the test, the Act-portion of a Unit Test is usually one line. It activates the portion of code being tested, passing in that data. Finally, the Assert portion of the test takes the Act portion results and verifies that it met expectations (can be zero lines when just making sure there is no error).

Unit Test generators generally can only do the Arrange, and Act portions on unit test creation; however, unit test generators generally do not write Assert portions as only you know what is correct and what is incorrect for your purposes. So some manual entry/extending of Unit Tests is necessary for completeness.

Solution 5 - C#

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.

Solution 6 - C#

http://www.parasoft.com">Parasoft .TEST has a functionality of tests generation. It uses NUnit framework for tests description and assertions evaluation.

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.

Solution 7 - C#

I've used NStub to stub out test for my classes. It works fairly well.

Solution 8 - C#

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.

Solution 9 - C#

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.

I don't know of a .net equivalent (Agitar did one announce a .net version but AFAIK it never materialised).

Solution 10 - C#

I know this thread is old but for the sake of all developpers, there is a good library called nunit :

https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.TestGeneratorNUnitextension

EDIT : Please use XUNIT ( Supported by microsoft ) : https://github.com/xunit/xunit

Autogenerated test : https://marketplace.visualstudio.com/items?itemName=YowkoTsai.xUnitnetTestGenerator

Good dev

Solution 11 - C#

Selenium generates unit tests from user commands on a web page, pretty nifty.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhobisView Question on Stackoverflow
Solution 1 - C#Paul BatumView Answer on Stackoverflow
Solution 2 - C#Jon LimjapView Answer on Stackoverflow
Solution 3 - C#N1ghtv1s1onView Answer on Stackoverflow
Solution 4 - C#johngView Answer on Stackoverflow
Solution 5 - C#tshakView Answer on Stackoverflow
Solution 6 - C#zuraffView Answer on Stackoverflow
Solution 7 - C#Hector Sosa JrView Answer on Stackoverflow
Solution 8 - C#S.LottView Answer on Stackoverflow
Solution 9 - C#RikalousView Answer on Stackoverflow
Solution 10 - C#BouamView Answer on Stackoverflow
Solution 11 - C#Chris JamesView Answer on Stackoverflow