Questions tagged [vs-unit-testing-framework]

Microsoft's unit testing framework

MSTest is Microsoft's unit testing framework, highly integrated into the Visual Studio IDE.

352 questions
957
votes
25 answers

How do I use Assert to verify that an exception has been thrown with MSTest?

How do I use Assert (or other Test class) to verify that an exception has been thrown when using MSTest/Microsoft.VisualStudio.TestTools.UnitTesting?
Alex
  • 75,813
  • 86
  • 255
  • 348
182
votes
9 answers

How can we run a test method with multiple parameters in MSTest?

NUnit has a feature called Values, like below: [Test] public void MyTest( [Values(1,2,3)] int x, [Values("A","B")] string s) { // ... } This means that the test method will run six times: MyTest(1, "A") MyTest(1, "B") MyTest(2,…
The Light
  • 26,341
  • 62
  • 176
  • 258
175
votes
4 answers

Why does TestInitialize get fired for every test in my Visual Studio unit tests?

I'm using Visual Studio 2010 Beta 2. I've got a single [TestClass], which has a [TestInitialize], [TestCleanup] and a few [TestMethods]. Every time a test method is run, the initialize and cleanup methods are ALSO run! I was under the impression…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
85
votes
2 answers

TestInitialize vs ClassInitialize

What is the difference between TestInitialize vs ClassInitialize in MSTest? What are the pros cons of each? I'm under the impression that TestInitialze should run with EACH test, every time? Is that correct? And that ClassInitialize will run…
snowmom475
  • 967
  • 1
  • 6
  • 7
72
votes
33 answers

Visual Studio - suddenly cannot debug tests

I'm not sure what I did, but all of a sudden, my Visual Studio 2012 will not debug any tests. If I select a test from the Test Explorer and tell it to run, it simply outputs this in the Test Output window: > ------ Discover test started ------ >…
65
votes
18 answers

MSTest: No tests are run because no tests are loaded or the selected tests are disabled

I have a c# solution with the following structure: mySolution myProject myProject.MSTests References Microsoft.VisualStudio.QualityTools.UnitTestFramework sutMSTests.cs sutMSTests.cs: [TestClass()] public class sutMSTests { …
gerryLowry
  • 2,626
  • 5
  • 35
  • 44
51
votes
7 answers

ClassInitialize attribute in unit test based class not called

I added these method in a TestBase class : [ClassInitialize] public static void InitializBeforeAllTests() { } But when I run in Debug an unit test Test1() : [TestClass] public class TestMapping : TestBase { [TestMethod] public void Test1() …
Phil
  • 1,035
  • 2
  • 10
  • 17
48
votes
3 answers

How can I default VS Test Processor Architecture to x64?

I depend on a mixed-mode assembly which is compiled for x64. Because I do not want warnings in my build, all of the projects in my solution target x64 directly--they are not Any CPU. To get my x64 unit tests to work properly, I find that I must…
47
votes
9 answers

How to access TestRunParameters within RunSettings file

Reading through https://msdn.microsoft.com/en-us/library/jj635153.aspx I have created a .RunSettings files with a few parameters similar to the example:
Kritner
  • 13,557
  • 10
  • 46
  • 72
47
votes
0 answers

Unit-Test project fails to build after installing .NET 4.5

I have a WPF solution in .NET 4.0 that includes a Unit Test project which tests the different commands used in the ViewModels. Everything was working fine, but then I installed .NET 4.5 and VS2012. Now when I try to build the solution I get error…
atiyar
  • 7,762
  • 6
  • 34
  • 75
45
votes
3 answers

Classes marked with TestInitialize and TestCleanup not executing

I have been struggling with this one, hopefully it will help someone else. Whilst creating unit tests using MsTest I discovered I was repeating the same code in each test, and found a couple of handy attributes (TestInitialize, TestCleanup,…
Stefan de Kok
  • 2,018
  • 2
  • 15
  • 19
41
votes
6 answers

VS2017 Could not load file or assembly Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll or one of its dependencies

When trying to open an older solution in VS2017 there is an old Unit Test project that is giving me a problem when building. I keep getting the following error when building this test project: Could not load file or assembly…
35
votes
5 answers

Model state validation in unit tests

I am writing a unit test for a controller like this: public HttpResponseMessage PostLogin(LoginModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); } the model looks like: public class…
33
votes
5 answers

Correct way to unit test the type of an object

Using the Visual Studio Unit Testing Framework, I'm looking at two options: Assert.AreEqual(myObject.GetType(), typeof(MyObject)); and Assert.IsInstanceOfType(myObject, typeof(MyObject)); Is there a difference between these two options? Is one…
drl
  • 811
  • 2
  • 11
  • 21
31
votes
14 answers

What could be causing a System.TypeLoadException in a Visual Studio Unit Test?

I've got a C# .NET class library MyClassLibrary that compiles fine. I'm trying to create a unit test project for it (using Visual Studio Unit Testing Framework, with Visual Studio 2010). The class library does have big classes in it, but whenever…
Jay Sullivan
  • 17,332
  • 11
  • 62
  • 86
1
2 3
23 24