MSTest Equivalent for NUnit's Parameterized Tests?
Would this help?
This week I was adding some unit tests
to a project that is managed by TFS,
so I decided to use the "core" unit
testing framework available with
VS2008, and unfortunately it doesn't
support RowTests. But it has a similar
feature called Data-Driven Unit Test.
With this approach it's a bit more
complicate to implement the "simple"
RowTest scenario, but it allows also
to implement more complicate ones.
Is there an MSTest equivalent to NUnit's Explicit Attribute?
No, the closest you will get is with the [Ignore] attribute.
However, MSTest offers other ways of disabling or enabling tests using Test Lists. Whether you like them or not, Test Lists are the recommended way to select tests in MSTest.
NUnit's TestCaseAttribute equivalent in VS Unit Test Framework
MSTest unfortunately doesn't support parameterized tests in this way. The closest you get is with the DataSource attribute, which allows you to specify an external data source for the test method.
Visual Studio unit testing - multiple cases like Nunit
But of course, one can use the DataRow
attribute as shown here:
[TestClass]
public class AdditionTests
{
[DataTestMethod]
[DataRow(1, 1, 2)]
[DataRow(2, 2, 4)]
[DataRow(3, 3, 6)]
public void AddTests(int x, int y, int expected)
{
Assert.AreEqual(expected, x + y);
}
}
NUnit multiple TestFixture equivalent for MsTest
There is no real equivalent. Closest I think is the Data Driven unit test.
What is the nUnit equivalent for a mstest XML file DataSource?
NUnit doesn't have direct support to read a custom xml file. However it does provide support to specify a method (refer for examples to the docs for the TestCaseSource attribute) which will compute test case inputs for a parameterized test.
You can implement this method to read the inputs from an XML / CSV or whatever custom logic you choose.
Something similar to NUnit TestCaseSource in Visual Studio unit testing
Datasource is one option to take the test input. You can save your test data in many file format (xml, xls or in sql db)
But if you don't want to store your test data, then use TestInitialize() attribute in any method that will execute before any of your test cases. Save all your test data run time in xml,xls or in sql db through this method and then using datasource use it in your test cases.
Here is msdn link for VSTS unit test. Hope this will help you.
Ms Test or NUnit?
They are pretty similar. Differences are subtle.
- NUnit has testcases for parametrized tests; MSTest does not.
You can write
[TestCase(1, "one)]
[TestCase(2, "two)]
[TestCase(3, "three)]
[TestCase(4, "four)]
public void CanTranslate(int number, string expectedTranslation)
{
var translation = _sut.Translate(number);
translation.Should().Be.EqualTo(expectedTranslation);
}
rather than writing 4 tests or using a cycle inside the test. Failing tests' error messages will be much clearer and test results will be always conveniently grouped.
- NUnit tends to be a bit more complete and flexible. For example, it comes with a cool set of attributes like Combinatorial.
For example
[Test, Combinatorial]
public void MyTest([Values(1,2,3)] int x, [Values("A","B")] string s)
{
...
}
which is equivalent to running the tests
MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
(see the original page here)
MSTest always instantiates a new instance of the test class for each test method being executed. This is very useful, since prior to each single test, Setup and TearDown methods will be run and every instance variables will be reset. With NUnit you have to take care of instance variables eventually shared between tests (though this shouldn't be a bis issue: a well designed test should be isolated by design)
- With NUnit an abstract classes can be a test fixtures and you can inherit other test fixtures from it. MsTest does not have this feature.
MSTest is well integrated with Visual Studio. You'd need a third party plugin to effectively work with NUnit, for example ReSharper, Test Driven .NET or NCrunch
NUnit has a fluent version of Assert, so you can write
for example
Assert.That(result).Is.GreaterThan(9)
rather than
Assert.Greater(9, result);
With SharpTestEx you can even write:
result.Should().Be.GreaterThan(9);
and take advantage of the strong typed IntelliSense.
Related Topics
How to Resolve Service for Type While Attempting to Activate
How to Take a Screenshot of a Wpf Control
How to Load Assembly at Runtime and Create Class Instance
C# Sortable Collection Which Allows Duplicate Keys
Break Out of a While Loop That Contains a Switch Statement
Initial Capacity of Collection Types, E.G. Dictionary, List
How to Automatically Display All Properties of a Class and Their Values in a String
Operation Could Destabilize the Runtime
How to Clear All Controls on a Form C#
Gmail Smtp via C# .Net Errors on All Ports
System.Net.Http: Missing from Namespace? (Using .Net 4.5)
Convert Datetime to Date Format Dd/Mm/Yyyy
Nullable Reference Types: How to Specify "T" Type Without Constraining to Class or Struct
How to List Directory Contents with Ftp in C#