How to Ignore in MSTests - c#

I'm sorry this seems like such a basic question but I can't find the answer anywhere, including the MS Docs which talk about it but don't give an actual example.
I just want to ignore some tests. Here are some things that don't seem to work:
[TestMethod]
[Ignore]
public void TestStartAcquireEmpty()
{
}
[TestMethod]
[IgnoreAttribute]
public void TestStartAcquireEmpty()
{
}
[Ignore]
[TestMethod]
public void TestStartAcquireEmpty()
{
}
[IgnoreAttribute]
[TestMethod]
public void TestStartAcquireEmpty()
{
}
If I use [Ignore] without [TestMethod] the test does disappear from the test explorer. But what I want is to get the yellow triangle in the test explorer.

To see the yellow triangle for a test it is necessary to run the test first.
The triangle appears then in the test explorer when a test method has the [Ignore] attribute or asserts with Assert.Inconclusive().
The MSTest Test Explorer is a unit test runner with the capability to discover test methods automatically (and a few more features).
Use Test Explorer to run unit tests from Visual Studio or third-party unit test projects. You can also use Test Explorer to group tests into categories, filter the test list, and create, save, and run playlists of tests.
https://learn.microsoft.com/de-de/visualstudio/test/run-unit-tests-with-test-explorer?view=vs-2019
Test methods are discovered when the are within a public class with the [TestClass] attribute and have the [TestMethod] attribute. They need to be public with return type void.
Discovered test are shown in the test explorer with a blue exclamation mark icon that symbols that the test didn't ran by now (If you deactivate the real time test discovery you need to compile the test project first to see the test methods in the test explorer).
If a test method has the [Ignore] attribute there is metadata added at compile time. That metadata is examined at runtime like most other attributes (see https://learn.microsoft.com/en-us/dotnet/api/system.attribute?view=netcore-3.1#remarks).
Therefore the attribute is examined at runtime it's necessary to run the test first to see the outcome in the test explorer.
If you want to see the outcome of test immediately you may try Visual Studio Live Unit Testing:
https://learn.microsoft.com/de-de/visualstudio/test/live-unit-testing?view=vs-2019

Related

What can cause the [Ignore] attribute to be ignored?

I have a test suite with a few tests that are failing because the requirements have changed out from under them, necessitating code changes that break the tests. It's not immediately obvious how to fix the tests, so for the moment I want to simply disable them.
I added the Microsoft.VisualStudio.TestTools.UnitTesting.IgnoreAttribute attribute to these tests, but they're still being run by Test Explorer. I've considered the possibility that the test runner we're using would use its own mechanism, but that seems unlikely, as it responds to the TestMethodAttribute and TestCategoryAttribute attributes from the same namespace. One of the tests looks like this:
[TestMethod]
[TestCategory("Integration")]
[Ignore]
public void TestJobIntegrationDev01()
{
//test code goes here
}
How do I determine why Ignore is not working in this case?

Run Selective Test Classes in VSTS

In Visual Studio Team Services (VSTS) when defining a build I can filter specific tests to be included or excluded when running tests.
Question: How do I filter complete test classes from execution? The example in the screenshot demonstrates how I filter tests based on their category.
Sample test class which I'd like to exclude:
[TestClass] // .NET 4.5
public class SampleTests
{
[TestMethod, TestCategory("Integration")]
public void Test1() {}
[TestMethod, TestCategory("Integration")]
public void Test2() {}
...
}
Current configuration to exclude my integration tests:
Trial: The filter criteria ClassName!=SampleTests doesn't work. It seems to be reserved for store apps only. Fairly good documentation here: MSDN Blog by Vikram Agrawal.
Reason for asking: I've got test classes initialize lots of data first before running any test and run a clean-up job at the end. When all my tests are excluded via the aforesaid filter the class initialization and clean-up still happen which consumes a lot of time and resources. I like to optimize this.
You can do this with:
FullyQualifiedName!=namespace.SampleTests

asp.net Unit Test: Mark test as incomplete

I'd like to unit test a asp.net MVC webapplication.
We're not using TDD (well, not yet).
After touching a method I'd like to mark the appropriate unit test as incomplete or something so the other team members know they have to complete it.
Is there any possibility to do so?
We're using the built in Unit test possibility in Visual Studio 2010.
Thanks in advance.
Michael
Do you want the tests to not actually be run until they've been worked on further? If so, there's an [Ignore] attribute that you can add to each test, as in (for MSTest):
[TestMethod, Ignore]
public void TestThatNeedsToBeCompleted()
{
}
If you're using NUnit, you can add a reason parameter to the Ignore attribute to explain why the test is being ignored. I don't think that's available in MSTest, but don't quote me on that :)
You can simply fail the test with assertion or throw NotImplementedException. And you will see that these tests are not ok.
Or eventually use the IgnoreAttribute to enable/disable the test when you need.
[Ignore]
[TestMethod]
public void TestMethod { }

Basic Unit Test vs. Unit Test

I am working on an MVC project and was wondering whether to use Basic Unit Test or Unit Test, I read articles / explanations about both but can't see much difference between the two. What are the main differences and which one is preferable for a large scale app with DB backend?
The difference between Visual Studio's Basic Unit Test item template and Unit Test item template is that the latter includes support for ClassInitialize, ClassCleanup, TestInitialize and TestCleanup routines allowing you to execute some code before/after the test fixture and some code before/after each unit test. If you don't need such functionality in your unit test you could go wit the basic template which generates the following file:
[TestClass]
public class UnitTest2
{
[TestMethod]
public void TestMethod1()
{
}
}
Of course you could always add the corresponding routines to a basic unit test if you want to support later this functionality.

Global.asax for Unit Tests?

In my MSTest UnitTest project, before running any tests, I need to execute some commands. Is there a feature, kind of like Global.asax is for web based projects, that will let me kick off something before any tests run?
I should make it clear that when I say "execute some commands", I don't mean DOS commands, but execute some code.
If I understand correctly, you need to have some initialization code run before you start your tests. If that is indeed the case you should declare a method inside your unit-test class with the ClassInitializeAttribute like this:
[ClassInitialize]
public void ClassSetUp()
{
//initialization code goes here...
}
Edit: there is also the AssemblyInitializeAttribute that will run before any other tests in assembly
Unit test frameworks usually support set up and "tear down" methods for both the entire test fixture and individual tests. MSTest lets you specify which methods to run when with these attributes:
[ClassIntialize()]
public void ClassInitialize() {
// MSTest runs this code once before any of your tests
}
[ClassCleanup()]
public void ClassCleanUp() {
// Runs this code once after all your tests are finished.
}
[TestIntialize()]
public void TestInitialize() {
// Runs this code before every test
}
[TestCleanup()]
public void TestCleanUp() {
// Runs this code after every test
}
Having said that, be careful with the class initialize and cleanup methods if you're running ASP.NET unit tests. As it says in the ClassInitializeAttribute documentation:
This attribute should not be used on
ASP.NET unit tests, that is, any test
with [HostType("ASP.NET")] attribute.
Because of the stateless nature of IIS
and ASP.NET, a method decorated with
this attribute may be called more than
once per test run.
properties of you project and then debug field there you can specify arguments
EDIT
When you see the debug menu in the properties you can start an external program to do certain things for you when you start debugging. This will trigger when you launch an instance of your test project. You can also specify command line arguments in the command line arguments box.
For example I use NUnit I specify NUnit as the external program and specify the location of the .dll in the command line arguments

Categories