A unit test generation extension for Visual Studio that aims to always produce code that compiles - covering the basic cases automatically and preparing as much as it can for the complex cases.
Get the extension from the Visual Studio Marketplace:
Alternatively see the releases on GitHub:
Demonstrates how tests are generated using MsTest for the test framework and Moq for the mocking framework
public interface IDependency
{
int Method();
}
public class TestClass
{
IDependency _dependency;
public TestClass(IDependency dependency)
{
_dependency = dependency;
}
public void SomeMethod(string methodName, int methodValue)
{
var x = _dependency.Method();
System.Console.WriteLine("Testing this" + x);
}
public System.Threading.Tasks.Task<int> SomeAsyncMethod(string methodName, int methodValue)
{
System.Console.WriteLine("Testing this");
return System.Threading.Tasks.Task.FromResult(0);
}
}
[TestClass]
public class TestClassTests
{
private TestClass _testClass;
private Mock<IDependency> _dependency;
[TestInitialize]
public void SetUp()
{
_dependency = new Mock<IDependency>();
_testClass = new TestClass(_dependency.Object);
}
[TestMethod]
public void CanConstruct()
{
// Act
var instance = new TestClass(_dependency.Object);
// Assert
Assert.IsNotNull(instance);
}
[TestMethod]
public void CannotConstructWithNullDependency()
{
Assert.ThrowsException<ArgumentNullException>(() => new TestClass(default(IDependency)));
}
[TestMethod]
public void CanCallSomeMethod()
{
// Arrange
var methodName = "TestValue237820880";
var methodValue = 1002897798;
_dependency.Setup(mock => mock.Method()).Returns(534011718);
// Act
_testClass.SomeMethod(methodName, methodValue);
// Assert
_dependency.Verify(mock => mock.Method());
Assert.Fail("Create or modify test");
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void CannotCallSomeMethodWithInvalidMethodName(string value)
{
Assert.ThrowsException<ArgumentNullException>(() => _testClass.SomeMethod(value, 1657007234));
}
[TestMethod]
public async Task CanCallSomeAsyncMethod()
{
// Arrange
var methodName = "TestValue1412011072";
var methodValue = 929393559;
// Act
var result = await _testClass.SomeAsyncMethod(methodName, methodValue);
// Assert
Assert.Fail("Create or modify test");
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public async Task CannotCallSomeAsyncMethodWithInvalidMethodName(string value)
{
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => _testClass.SomeAsyncMethod(value, 760389092));
}
}