As opposed to the other frameworks which only allow parallelization at the class level. So, for example, If you have 100 test methods in 5 classes, MSTest will let you run 100 tests in parallel. However, with NUnit, you can only run the 5 tests in parallel, one from each test class at the time.
To run MSTest unit tests, specify the full path to the MSTest executable (mstest.exe) in the Unit Testing Options dialog. To call this dialog directly from the editor, right-click somewhere in the editor and then click Options.
The TestClass attribute denotes a class that contains unit tests. The TestMethod attribute indicates a method is a test method.
EDIT 4: Looks like this is completed in MSTest V2 June 17, 2016: https://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/
Original Answer:
As of about a week ago in Visual Studio 2012 Update 1 something similar is now possible:
[DataTestMethod]
[DataRow(12,3,4)]
[DataRow(12,2,6)]
[DataRow(12,4,3)]
public void DivideTest(int n, int d, int q)
{
Assert.AreEqual( q, n / d );
}
EDIT: It appears this is only available within the unit testing project for WinRT/Metro. Bummer
EDIT 2: The following is the metadata found using "Go To Definition" within Visual Studio:
#region Assembly Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll, v11.0.0.0
// C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework\11.0\References\CommonConfiguration\neutral\Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll
#endregion
using System;
namespace Microsoft.VisualStudio.TestPlatform.UnitTestFramework
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DataTestMethodAttribute : TestMethodAttribute
{
public DataTestMethodAttribute();
public override TestResult[] Execute(ITestMethod testMethod);
}
}
EDIT 3: This issue was brought up in Visual Studio's UserVoice forums. Last Update states:
STARTED · Visual Studio Team ADMIN Visual Studio Team (Product Team, Microsoft Visual Studio) responded · April 25, 2016 Thank you for the feedback. We have started working on this.
Pratap Lakshman Visual Studio
https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/3865310-allow-use-of-datatestmethod-datarow-in-all-unit
This feature is in pre-release now and works with Visual Studio 2015.
For example:
[TestClass]
public class UnitTest1
{
[TestMethod]
[DataRow(1, 2, 2)]
[DataRow(2, 3, 5)]
[DataRow(3, 5, 8)]
public void AdditionTest(int a, int b, int result)
{
Assert.AreEqual(result, a + b);
}
}
It is unfortunately not supported in older versions of MSTest. Apparently there is an extensibility model and you can implement it yourself. Another option would be to use data-driven tests.
My personal opinion would be to just stick with NUnit though...
As of Visual Studio 2012, update 1, MSTest has a similar feature. See McAden's answer.
Not exactly the same as NUnit's Value
(or TestCase
) attributes, but MSTest has the DataSource
attribute, which allows you to do a similar thing.
You can hook it up to database or XML file - it is not as straightforward as NUnit's feature, but it does the job.
MSTest has a powerful attribute called DataSource. Using this you can perform data-driven tests as you asked. You can have your test data in XML, CSV, or in a database. Here are few links that will guide you
It's very simple to implement - you should use TestContext
property and TestPropertyAttribute
.
public TestContext TestContext { get; set; }
private List<string> GetProperties()
{
return TestContext.Properties
.Cast<KeyValuePair<string, object>>()
.Where(_ => _.Key.StartsWith("par"))
.Select(_ => _.Value as string)
.ToList();
}
//usage
[TestMethod]
[TestProperty("par1", "http://getbootstrap.com/components/")]
[TestProperty("par2", "http://www.wsj.com/europe")]
public void SomeTest()
{
var pars = GetProperties();
//...
}
I prepared few extension methods to simplify access to the TestContext
property and act like we have several test cases. See example with processing simple test properties here:
[TestMethod]
[TestProperty("fileName1", @".\test_file1")]
[TestProperty("fileName2", @".\test_file2")]
[TestProperty("fileName3", @".\test_file3")]
public void TestMethod3()
{
TestContext.GetMany<string>("fileName").ForEach(fileName =>
{
//Arrange
var f = new FileInfo(fileName);
//Act
var isExists = f.Exists;
//Asssert
Assert.IsFalse(isExists);
});
}
and example with creating complex test objects:
[TestMethod]
//Case 1
[TestProperty(nameof(FileDescriptor.FileVersionId), "673C9C2D-A29E-4ACC-90D4-67C52FBA84E4")]
//...
public void TestMethod2()
{
//Arrange
TestContext.For<FileDescriptor>().Fill(fi => fi.FileVersionId).Fill(fi => fi.Extension).Fill(fi => fi.Name).Fill(fi => fi.CreatedOn, new CultureInfo("en-US", false)).Fill(fi => fi.AccessPolicy)
.ForEach(fileInfo =>
{
//Act
var fileInfoString = fileInfo.ToString();
//Assert
Assert.AreEqual($"Id: {fileInfo.FileVersionId}; Ext: {fileInfo.Extension}; Name: {fileInfo.Name}; Created: {fileInfo.CreatedOn}; AccessPolicy: {fileInfo.AccessPolicy};", fileInfoString);
});
}
Take a look to the extension methods and set of samples for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With