I have structured my Tests in Visual Studio like so:
[TestClass]
public class TestContainer
{
[TestClass]
public class MoreSpecificContainer
{
[TestMethod]
public void Test_Should_ReturnSomething()
{
}
}
[TestClass]
public class AnotherSpecificContainer
{
[TestMethod]
public void AnotherTest_Should_ReturnSomething()
{
}
}
}
Sadly, OpenCover (great tool, btw!) is not including the nested TestClass methods in it's coverage. I assume this is because they're nested. If I remove the nested 'MoreSpecificContainer' and 'AnotherSpecificContainer' classes and the test methods are only inside the main 'TestContainer' TestClass, then the tests are run by OpenCover and results are included in the reporting perfectly. Using OpenCover 4.6.166 and ReportGenerator.2.3.2.0
Is there any way that anybody knows of to configure OpenCover to notice the methods inside nested TestClasses?
Thank you,
codenewbie
OpenCover doesn't run your tests, the application you use for target does.
Try running your tests on the command line without OpenCover and review your results.
Related
I want to put Unit Test to this class with NUnit, how do I do it?
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Universal.DataTransferNCFDGII.Function.Services;
[assembly: FunctionsStartup(typeof(Universal.DataTransferNCFDGII.Function.Startup))]
namespace Universal.DataTransferNCFDGII.Function
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IInsertData, InsertData>();
}
}
}
Generally you do not test classes and methods Startup and Configure as there is no real logic of yours in need of testing. You would just be testing that the dependency injection setup works and that is not your code. It is a good practice while writing tests to remember to only test your code, not third party or .NET libraries. It would be better to cover this with an integration test, rather than unit test.
If you are concerned with no test for this class and method impacting your code coverage, you can just put the ExcludeFromCodeCoverage attribute on your method or class, like this:
[ExcludeFromCodeCoverage]
public override void Configure(IFunctionsHostBuilder builder)
I used to be able to hit Ctrl+U,R to run all test classes inside a given file using ReSharper (NUnit is mytest harness).
For example, here is my class layout:
public static class TestsForT
{
[TestFixture]
public class WhenTDoesThis
{
[Test]
public void ThisHappens()
{
}
}
[TestFixture]
public class WhenTDoesThat
{
[Test]
public void ThatHappens()
{
}
}
}
All I would have to do was to place my cursor at the top of the file (over the static class declaration), then hit Ctrl+U,R and ReSharper would look at ALL the TestFixtures in my file and run all of them for me.
Now, unless my cursor is directly on the same line as the [TestFixture] attribute, ReSharper cannot run all unit tests in the file. If can only run one test at a time.
What magic ReSharper setting am I missing here? I don't want to run each unit test one at a time, b/c we can have up to 30 classes with the [TextFixture] attribute in one static test class.
Any ideas?
Kote answered by question. Basically locate the test file using Shift+Alt+L. You'll see it highlighted in Solution Explorer. then hit Ctrl+U,R and that will run all tests in the file.
I have two classes TestClass1, TestClass2
I have set MaxParallel threads through assembly in AssemblyInfo.cs file in my test project:
[assembly: Xunit.CollectionBehaviorAttribute(MaxParallelThreads = 4)]
Reference Set maximum parallel threads
I have installed xunit-2.0.0-beta4-build2738(Prerelease). Also installed Xunit runner to find tests.
From my knowledge and research tells that Xunit does not run tests in the same collection in parallel. Reference Using test collections in xUnit.net v2 that is why for both the classes below I have used different collections.
Visual Studio 2013 finds my tests but when I run all the tests, it still runs tests serially. I want them to run in parallel.
If someone could help that would be great!
My code below:
Namespace1
namespace name1
{
public class MFixture
{
}
[CollectionDefinition("Test1")]
public class CollectionClass : ICollectionFixture<MFixture>
{
}
[Collection("Test1")]
public class TestClass1
{
// variables
public TestClass1()
{
//code
}
[Fact]
public void method1()
{
//code
}
[Fact]
public void method2()
{
//code
}
}
}
Namespace2
namespace name2
{
public class MFixture1
{
}
[CollectionDefinition("Test2")]
public class CollectionClass1 : ICollectionFixture<MFixture1>
{
}
[Collection("Test2")]
public class TestClass2
{
// variables
public TestClass2()
{
//code
}
[Fact]
public void method12()
{
//code
}
[Fact]
public void method22(){
{
//code
}
}
}
Other references
How to run Selenium tests in parallel with xUnit
XUnit.net attributes
XUnit.net issues
In xUnit, Each test class is a unique test collection. Tests within the same test class will not run in parallel against each other.
So in order to achieve that, you need to keep your tests in different collection(which you want to run in parallel), however that is not efficient way to solve your issue.
I answered this as per my knowledge and the content which I got on Internet, you can also refer to this link
It seems that Visual Studio doesn't support running tests in parallel. I haven't tested with the exact versions you asked about, but have found other comments saying as much. As suggested, running from the command line should work.
For me, I'm using VS2015, dnx 1.0.0-rc1-final, xunit 2.1.0, xunit.runner.dnx 2.1.0-rc1-build204 and can confirm that VS Test Explorer is dumb.
According to the docs:
By default, each test class is a unique test collection.
So given some contrived "tests" like these, it should be easy to see if they run in parallel or not:
Visual Studio:
Command line:
Can someone suggest a way to run tests in a specific order in Visual Studio 2013 Express?
Is there a way to create a playlist for the tests, which also defines the order in which to run them?
By the way: These are functional tests, using Selenium, written as unit tests in C#/Visual Studio. Not actual unit tests. Sometimes a regression test suite is so big it takes a while to run through all the tests. In these cases, I've often seen the need to run the test in a prioritized order. Or, there can be cases where it's difficult to run some tests without some other tests having been run before. In this regard, it's a bit more complicated than straight unit tests (which is the reason why it's normally done by test professionals, while unit tests are done by developers).
I've organised the tests in classes with related test methods. Ex.: All login tests are in a class called LoginTests, etc.
Class LoginTests:
- AdminCanLogin (...)
- UserCanLogin (...)
- IncorrectLoginFails (...)
- ...
CreatePostTests
- CanCreateEmptyPost (...)
- CanCreateBasicPost (...)
...
These classes are unit test classes, in their own project. They in turn calls classes and methods in a class library that uses Selenium.
MS suggests creating an "Ordered Unit Test" project. However, this is not available in the Express edition.
To address your playlist request directly see MS article: http://msdn.microsoft.com/en-us/library/hh270865.aspx Resharper also has a nice test play list tool as well.
Here is an article on how to setup Ordered Tests but you cannot use this feature with Express as it requires Visual Studio Ultimate, Visual Studio Premium, Visual Studio Test Professional. http://msdn.microsoft.com/en-us/library/ms182631.aspx
If you need them ordered then they are more then likely integration tests. I am assuming you would like them ordered so you can either prepare data for the test or tear data back down after the test.
There are several ways to accommodate this requirement if it is the case. Using MSTest there are 4 attributes for this you can see more details of when they are executed here http://blogs.msdn.com/b/nnaderi/archive/2007/02/17/explaining-execution-order.aspx.
My other suggestion would be to have a helper class to preform the tasks(not tests) you are looking to have done in order, to be clear this class would not be a test class just a normal class with common functionality that would be called from within your tests.
If you need a test to create a product so another test can use that product and test that it can be added to a shopping cart then I would create a "SetupProduct" method that would do this for you as I am sure you would be testing various things that would require a product. This would prevent you from having test dependencies.
With that said, integration tests are good to verify end to end processes but where possible and applicable it might be easier to mock some or all dependencies such as your repositories. I use the Moq framework and find it really easy to work with.
This code is from the blog post linked above, I am placing it here in case the link ever dies.
Here is an example of a test class using the setup / tear down attributes to help with your tests.
[TestClass]
public class VSTSClass1
{
private TestContext testContextInstance;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[ClassInitialize]
public static void ClassSetup(TestContext a)
{
Console.WriteLine("Class Setup");
}
[TestInitialize]
public void TestInit()
{
Console.WriteLine("Test Init");
}
[TestMethod]
public void Test1()
{
Console.WriteLine("Test1");
}
[TestMethod]
public void Test2()
{
Console.WriteLine("Test2");
}
[TestMethod]
public void Test3()
{
Console.WriteLine("Test3");
}
[TestCleanup]
public void TestCleanUp()
{
Console.WriteLine("TestCleanUp");
}
[ClassCleanup]
public static void ClassCleanUp()
{
Console.WriteLine("ClassCleanUp");
}
}
Here is the order that the methods were fired.
Class Setup
Test Init
Test1
TestCleanUp
Test Init
Test2
TestCleanUp
Test Init
Test3
TestCleanUp
ClassCleanUp
If you give more information on what you are trying to accomplish I would be happy to assist you in when to use which attribute or when to use the help class, note the helper class is NOT a test class just a standard class that has methods you can utilize to do common tasks that may be needed for multiple tests.
WHy is it not possible to inheritance tests from other assemblies to run:
namespace TestProject.Base
{
[TestClass]
public abstract class TestBaseClass
{
[TestMethod]
public void BaseTest()
{
Assert.IsTrue(false);
}
}
}
Test Runner
namespace TestProject.UnitTest
{
[TestClass]
public class UnitTest : TestBaseClass
{
}
}
It is ONLY possible to do run the test when the classes are in the SAME assembly WTF!
Is it possible to have test inheritance like above with NUnit and be runnable?
Unfortunately it seems this is a limitation of the MSUnit framework. In the MSDN documentation it states clearly that tests can inherit methods from another test class in the same assembly (see http://msdn.microsoft.com/en-us/library/ms182517.aspx).
Probably the cause for this is the way MSUnit discovers tests and so far it seems there is no workaround (other than having a copy of the test files in the project).