Questions tagged [mstest]
Microsoft .NET Unit Testing Framework (MSTest)
mstest
3,646
questions
965
votes
25
answers
804k
views
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?
454
votes
34
answers
314k
views
Entity Framework Provider type could not be loaded?
I am trying to run my tests on TeamCity which is currently installed on my machine.
System.InvalidOperationException:
The Entity Framework provider type
'System.Data.Entity.SqlServer....
421
votes
19
answers
380k
views
Unit testing private methods in C#
Visual Studio allows unit testing of private methods via an automatically generated accessor class. I have written a test of a private method that compiles successfully, but it fails at runtime. A ...
414
votes
7
answers
142k
views
NUnit vs. MbUnit vs. MSTest vs. xUnit.net [closed]
There are quite a lot of unittesting frameworks out there for .NET. I found this little feature comparison: http://xunit.github.io/docs/comparisons.html
Now I am to choose the best one for us. But how?...
316
votes
17
answers
130k
views
Your project does not reference ".NETFramework,Version=v4.6.2" framework. Add a reference to ".NETFramework,Version=v4.6.2" in the "TargetFrameworks"
I can't run my unit tests.
I have the next error:
Your project does not reference ".NETFramework,Version=v4.6.2"
framework. Add a reference to ".NETFramework,Version=v4.6.2" in the
"...
269
votes
4
answers
99k
views
What would be an alternate to [TearDown] and [SetUp] in MSTest?
When I use MSTest Framework, and copy the code that Selenium IDE generated for me, MSTest doesn't recognize [TearDown] and [SetUp]. What is the alternative to this?
231
votes
7
answers
148k
views
How to compare Lists in Unit Testing
How can this test fail?
[TestMethod]
public void Get_Code()
{
var expected = new List<int>();
expected.AddRange(new [] { 100, 400, 200, 900, 2300, 1900 });
var actual = new List<...
216
votes
25
answers
204k
views
Unit Testing: DateTime.Now
I have some unit tests that expects the 'current time' to be different than DateTime.Now and I don't want to change the computer's time, obviously.
What's the best strategy to achieve this?
187
votes
11
answers
218k
views
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 ...
176
votes
4
answers
86k
views
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 ...
171
votes
17
answers
192k
views
How can I write output from a unit test?
Any call in my unit tests to either Debug.Write(line) or Console.Write(Line) simply gets skipped over while debugging and the output is never printed. Calls to these functions from within classes I'm ...
155
votes
9
answers
74k
views
Where is the "Create Unit Tests" selection?
I have installed the new Visual Studio 2012 Ultimate.
I have created a Test Project with my solution and it contains some default unit tests. However right clicking on new Methods doesn't show the "...
152
votes
6
answers
58k
views
Does MSTest have an equivalent to NUnit's TestCase?
I find the TestCase feature in NUnit quite useful as a quick way to specify test parameters without needing a separate method for each test. Is there anything similar in MSTest?
[TestFixture]
...
134
votes
6
answers
146k
views
How to write to Console.Out during execution of an MSTest test
Context:
We have some users reporting issues with a file upload feature in our web application. It only happens occasionally and without any special pattern. We have been trying to figure it out for a ...
131
votes
3
answers
57k
views
Is it possible to execute code once before all tests run?
Basically I would like to tell MSTest to execute a bit of code before launching into a series of test runs, essentially what I would like to do is the same thing as sticking some code in Main().
The ...
129
votes
11
answers
78k
views
How can I get "Copy to Output Directory" to work with Unit Tests?
When I build a unit test project before the tests are executed the test output is copied to a TestResults folder and then the tests are executed. The issue I'm having is that not all the files in the ...
123
votes
7
answers
32k
views
How can I reset an EF7 InMemory provider between unit tests?
I am trying to use the EF7 InMemory provider for unit tests, but the persistent nature of the InMemory database between tests is causing me problems.
The following code demonstrates my issue. One test ...
115
votes
8
answers
61k
views
Can I use mstest.exe without installing Visual Studio?
I want to use mstest.exe to run my unit test on build server, but I don't want to install Visual Studio on the build server. Can I just install MSTest without Visual Studio?
113
votes
6
answers
40k
views
MSTest copy file to test run folder
I've got a test which requires an XML file to be read in and then parsed. How can I have this file copied into the test run folder each time?
The XML file is set to "Copy if newer" and a compile mode ...
109
votes
9
answers
104k
views
How do I check "no exception occurred" in my MSTest unit test?
I'm writing a unit test for this one method which returns "void". I would like to have one case that the test passes when there is no exception thrown. How do I write that in C#?
Assert.IsTrue(????)...
102
votes
2
answers
52k
views
Visual Studio Unit Testing: SetUp and TearDown
Instead of [SetUp] and [TearDown] in Nunit what is the alternative in Visual Studio Ultimate 2010 Unit Testing. In Nunit you can imagine setup and teardown methods are as constructors and destructors ...
101
votes
19
answers
34k
views
VS 2010 Test Runner error "The agent process was stopped while the test was running."
In Visual Studio 2010, I have a number of unit tests. When I run multiple tests at one time using test lists, I sometimes reveive the following error for one or more of the tests:
The agent process ...
100
votes
20
answers
90k
views
Problems with DeploymentItem attribute
I'm currently maintaining an "old" system written in C# .NET, removing some obsolete features and doing some refactoring. The previous guy wrote some unit tests (MSTests). I quite ...
94
votes
4
answers
5k
views
Why does this assert throw a format exception when comparing structures?
I'm trying to assert the equality of two System.Drawing.Size structures, and I'm getting a format exception instead of the expected assert failure.
[TestMethod]
public void AssertStructs()
{
var ...
87
votes
5
answers
48k
views
MsTest ClassInitialize and Inheritance
I have a base class for my tests which is composed in the following way:
[TestClass]
public abstract class MyBaseTest
{
protected static string myField = "";
[ClassInitialize]
public static ...
86
votes
2
answers
48k
views
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 ...
81
votes
11
answers
63k
views
Do you use TestInitialize or the test class constructor to prepare each test? and why?
This question regards unit testing in Visual Studio using MSTest (this is important, because of MSTest's execution order). Both the method marked [TestInitialize] and the test class constructor will ...
80
votes
8
answers
31k
views
Unit test exception messages with xUnit
I'm currently converting my MsTest unit tests to xUnit. With xUnit, is there a way to test exception messages? Is it correct to test exception messages as opposed just the exception type?
78
votes
3
answers
30k
views
Global test initialize method for MSTest
Quick question, how do I create a method that is run only once before all tests in the solution are run.
77
votes
7
answers
39k
views
Do MSTest deployment items only work when present in the project test settings file?
I can't seem to grasp how MSTest deployment items are supposed to be configured. I have been able to get them working properly by modifying the project's test settings file, but this is less then ...
76
votes
5
answers
35k
views
MSTest Equivalent for NUnit's Parameterized Tests?
NUnit supports a feature where you can specify a set of data inputs for a unit test to be run multiple times.
[RowTest]
[Row(1001,1,2,3)]
[Row(1,1001,2,3)]
[Row(1,2,1001,3)]
public void SumTests(int ...
75
votes
33
answers
86k
views
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 ...
75
votes
9
answers
49k
views
Where is mstest.exe located?
I need to run mstest from the command line - where in the world is this exe located? Can anyone give me a clue?
Edit: I only have Visual Studio 2010 installed
73
votes
13
answers
33k
views
Can't read app.config in C# .NET Core unit test project with ConfigurationManager
I've created a simple unit test project to read an app.config file. Target framework is Core 2.0. I also created a Core 2.0 console app, to sanity-check myself to make sure I wasn't doing anything ...
71
votes
2
answers
60k
views
Visual Studio 2013 MSTest vs NUnit
My company is upgrading our Visual Studio 2012 to 2013 Premium. In the midst of this, we are also looking to start automating our tests using Visual Studio Team Services
I have read a couple of ...
67
votes
3
answers
91k
views
DataTestMethod and DataRow attributes in MSTEST
I have seen in a Microsoft video about Visual Studio update 2 regarding these attributes. However, I can't find any other information about them and can't get a project to build with them.
Does ...
66
votes
13
answers
43k
views
Visual Studio 15.8.1 not running MS unit tests
When I updated Visual Studio to the latest version, 1 of my test projects stopped running tests and outputted this message:
Test project {} does not reference any .NET NuGet adapter. Test
discovery ...
65
votes
18
answers
62k
views
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....
63
votes
8
answers
82k
views
Stepping through and debugging code in Unit tests
I have not been able to debug or step through unit test.
Here is my sample test code...
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft....
63
votes
4
answers
32k
views
Why does Visual Studio create a new .vsmdi file?
If I open a solution in Visual Studio 2008 and run a unit test then VS creates a new .vsmdi file in the Solution Items folder and gives it the next number available e.g. My Solution2.vsmdi.
Any idea ...
62
votes
10
answers
17k
views
MSTest run fails because source assembly is not trusted
I just added xUnit to our test project (for the Asserts, we're still using MSTest as the framework) and immediately the test runs refused to execute any of the tests. This is the error message:
...
62
votes
5
answers
20k
views
How to set up LocalDb for unit tests in Visual Studio 2012 and Entity Framework 5
We have a Visual Studio 2012 ASP.NET MVC project using Entity Framework 5.
There are some unit tests that depend on a database. Setting up the app.config file in the test project to use a central SQL ...
61
votes
3
answers
10k
views
xUnit Equivalent of MSTest's Assert.Inconclusive
What is the xUnit equivalent of the following MSTest code:
Assert.Inconclusive("Reason");
This gives a yellow test result instead of the usual green or red. I want to assert that the test ...
56
votes
5
answers
88k
views
Ignore IgnoreAttribute
We have MSTest tests which automatically run in hourly production.
One of these tests is marked with [Ignore] attribute because it is not yet ready to run it in our production environment.
Now I want ...
56
votes
1
answer
3k
views
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.
...