I have installed JetBrains' DotCover and ReSharper installed in Visual Studio 2019.
Unfortunatelly the DotCover code coverage seems to be not working. I have this sample class:
using System;
namespace ClassLibrary1
{
public class Class1
{
public int X { get; set; }
public int Y { get; set; }
public int Division()
{
return X / Y;
}
}
}
And this sample unit test:
using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var c = new Class1 {X = 10, Y = 2};
var d = c.Division();
Assert.AreEqual(d, 5);
}
}
}
Then in ReSharper's "Unit Test Sessions" window, I select "Cover Unit Tests" as shown below:
This action runs my tests and when I move to ReSharper's "Unit Test Coverage" window, I see all coverage percentages as 0% and a warning message stating "Coverage info for some tests is absent or outdated", as shown below:
Also, in the Visual Studio code editor window, all the statements in my class are marked as "Statement uncovered" as shown below:
So, for some reason dotCover seems to be not working. I tried dropping the coverage data and running the tests again, but the result is the same.
What am I missing?
I just ran into something similar. In my case, I had just added a test using MSTest instead of MSTestV2 for my unit tests and code coverage stopped working. I switched to MSTestV2 by adding the following nuget packages:
MSTest.TestAdapter (v2.1.2)
and
MSTest.TestFramework (v2.1.2)
and removing the project reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.
Hopefully this helps someone!
Also, I'm on Visual Studio 2017 Professional 15.9.26 and dotCover 2020.2 if that makes a difference.
Related
My solution has a project that contains all the program logic.
I created a unit test project, added a reference to the main project, but still can't use classes from it to create tests.
My code:
namespace Program
{
public class Class
{
public Class()
{
///
}
public int foo()
{
///
}
}
}
My tests code:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Program; // cs0246
namespace ProgramTests
{
[TestClass]
public class ClassTests
{
[TestMethod]
public void foo_()
{
// Arrange
Class testClass; // this code also have cs0246 error
// Act
// Assert
}
}
}
In this code, using Program; underlined in red with cs0246 error. But namespace ProgramTests have the reference to Program (there is a checkmark in the reference manager). How can i fix it?
Image of Solution Explorer
Please, check the framework version you are using. Maybe Tests library has framework which is no compatible with ClassLib Framework
When developing a web app, quite often we need to do unit testing against objects in class libraries. I run into the problem when trying to use Xunit "CollectionDefinition" feature to share content between multiple test cases. (see details in following sections)
TEST ENVIRONMENT
Windows 11 and Visula Studio 2022 community edition
.NET 6 Web App "XunitDemo" with a .NET 6 Class Library "DataHelper"
Packages:
Xunit 2.4.2
Xunit.runner.visualstudio 2.4.3
PROBLEM DESCRIPTION:
In order to test sharing content between following test cases, I wrote test code with following components
The class to be tested: DataHelper.TextHelper (see code segment 1)
Test project: DataHelperTests
In test project, created the "fixture" object TextHelperFixture (see code segment 2)
In test project, Created the xunit CollectionDefinition (see code segment 3)
To make sure the TextHelperFixture works correctly, I setup first test case: TextHelperFixtureTest (see code segment 4)
To test the content sharing between multiple test cases, I setup 2 separate test cases: CollectionDefTest1 & CollectionDefTest2 with [Collection(....)] attribute. Each has 1 test method (see code segments 5 and 6)
Testing Steps:
To verify the "fixture" object actually works, I run TextHelperFixtureTest and it passed with no error.
To test the "CollectionDefinition", I tried to execute first test method: CollectDefTest1.CollDefTest1. It failed with following error:
DataHelperTests.CollectionDefTest1.CollDefTest1
Source: CollectionDefTest1.cs line 15
Duration:1 ms
Message: The following constructor parameters did not have matching fixture data: TextHelperFixture fixture
When running second test with CollectionDefTest2.CollDefTest2, the similar error occurred:
DataHelperTests.CollectionDefTest2.CollDefTest1
Source: CollectionDefTest2.cs line 16
Duration: 1 ms
Message: The following constructor parameters did not have matching fixture data: TextHelperFixture thFixture
QUESTIONS
The test result of "TextHelperFixtureTest" showed that the "Fixture" object is working fine when it was directly instantiated inside of the test case. Why using the "CollectionDefinition" caused the error of unable to find the "Fixture" class?
I thought that it was possible the CollectionDefinition won't work with class library. I actually setup a console app with class library, The "CollectionDefinition" was working fine there. Why this problem occurs when the main project is a .NET 6 web app?
It will be greatly appreciated if anyone has suggestions and troubleshooting tips.
TEST CODE SEGMENTS
------ Code Segment 1: Class in library to be Tested ------
namespace DataHelper
{
public class TextHelper
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public TextHelper() { }
public TextHelper (string fn, string ln)
{
FirstName = fn;
LastName = ln;
}
public string GetFullName()
{
return $"{FirstName} {LastName}";
}
}
}
------ Code Segment 2: "Fixture" object in test project ------
using System;
namespace DataHelperTests
{
public class TextHelperFixture: IDisposable
{
public string FullName { get; private set; }
public TextHelperFixture()
{
TextHelper st = new TextHelper("Jason", "Williams");
FullName = st.GetFullName();
}
public void Dispose()
{
}
}
}
------ Code Segment 3: CollectionDefinition in Test project ------
using Xunit;
namespace DataHelperTests
{
[Collection("TextHelper Collection")]
public class TextHelperFixtureCollection : ICollectionFixture<TextHelperFixture> { }
}
------ Code Segment 4: Test case to verify "fixture" object ------
using Xunit;
namespace DataHelperTests
{
public class TextHelperFixtureTest
{
private TextHelperFixture _fxt;
public TextHelperFixtureTest()
{
_fxt = new TextHelperFixture();
}
[Fact]
public void FixtureTest()
{
Assert.NotNull(_fxt.FullName);
}
}
}
------ Code Segment 5: first test utilizes the Collection ------
using Xunit;
namespace DataHelperTests
{
[Collection("TextHelper Collection")]
public class CollectionDefTest1
{
private TextHelperFixture _fixture;
public CollectionDefTest1(TextHelperFixture fixture)
{
_fixture = fixture;
}
[Fact]
public void CollDefTest1()
{
string result = _fixture.FullName;
Assert.Contains("Jason", result);
}
}
}
------ Code Segment 6: second test case that ultilizes the collection ------
using Xunit;
namespace DataHelperTests
{
[Collection("TextHelper Collection")]
public class CollectionDefTest2
{
private TextHelperFixture _fixture;
public CollectionDefTest2(TextHelperFixture thFixture)
{
_fixture = thFixture;
}
[Fact]
public void CollDefTest1()
{
Assert.NotNull(_fixture.FullName);
}
}
}
You've done 99% of it perfectly (it's a pleasure to answer a question where someone has clearly taken the time to surface as much as possible in textbook fashion like here) - just a typo/mistake under
Code Segment 3: CollectionDefinition in Test project
you are using the [Collection] attribute as one correctly would on the Test Class, but you meant [CollectionDefinition], which is the counterpart for the definition bit...
(Putting this in long form for future readers - you obviously had it all sorted but just typo'd in this instance)
EDIT:
I might have found a possible lead.
Checking my Test Runs it would seem as if I have 0 completed runs (even though all my 15 tests complete). Any clues?
I've got a technical interview coming up where testing will be the main focus. I've got rather limited exposure to testing in Visual Studio and can't seem to figure out why my version (VS2017) won't display the output button when I run tests.
Since my limited exposure I've been following along a few PluralSight courses on the subject and have found a decent one covering both LINQ and VS's own unit testing framework.
This is where it should be on VS2015 (I think?):
And this is how it looks for me:
As you can see I'm missing the output button for some god forsaken reason. I've looked in multiple windows (output's debug and tests), but simply cannot see the output.
My unit test follows the instructor's structure with some small changes (like how I set up my TestContext, which follows the structure of this answer to a similar question..
This is my unit test:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ACM.Library.Test
{
[TestClass]
public class BuilderTest
{
private TestContext _testContextInstance;
public TestContext TestContext
{
get { return _testContextInstance; }
set { _testContextInstance = value; }
}
public Builder listBuilder { get; set; }
[TestInitialize]
public void TestInitialize ()
{
this.listBuilder = new Builder();
}
[TestMethod]
public void TestBuildIntegerSequence()
{
var result = listBuilder.BuildIntegerSequence();
foreach(var item in result)
{
TestContext.WriteLine(item.ToString());
}
Assert.IsNotNull(result);
}
}
}
EDIT:
Here is the function I'm testing...
public IEnumerable<int> BuildIntegerSequence()
{
var integers = Enumerable.Range(0, 10);
return integers;
}
I have a simple NUnit project in C#. I have a file TestFixture.cs that successfully runs its four tests. If I want to add additional tests in a new CS file, how do I do this? If I simply copy the code from TestFixture.cs into a new TestFixture2.cs and rename the TestFixture classes to TestFixture3 and 4 respectively, they do not execute. I would think they would automatically run. Do I need to tell the runner specifically if it is a file other than TestFixture.cs? Here is my code:
namespace NUnitTest1
{
[TestFixture]
public class TestFixture3
{
[Test]
public void TestTrue()
{
Assert.IsTrue(true);
}
// This test fail for example, replace result or delete this test to see all tests pass
[Test]
public void TestFault()
{
Assert.IsTrue(true);
}
}
[TestFixture]
public class TestFixture4
{
[Test]
public void TestTrue()
{
Assert.IsTrue(true);
}
// This test fail for example, replace result or delete this test to see all tests pass
[Test]
public void TestFault()
{
Assert.IsTrue(true);
}
}
}
My unit test runs by executing a command line program using the following code:
namespace NUnitTest1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string[] my_args = { Assembly.GetExecutingAssembly().Location };
int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);
if (returnCode != 0)
Console.Beep();
}
}
}
NUnit will automatically get all types marked with TestFixture attribute from test assembly when you load it (it does not matter whether fixtures in one .cs file or in separate). Then NUnit will search for methods marked with Test or TestCase attributes and load them. If NUnit don't see some test, then make sure you loaded latest version of your test assembly.
NOTE: If you are using NUnit test runner, then there is nice setting Reload when test assembly changes. With this option turned on NUnit will automatically reload test assembly when you rebuild it in Visual Studio.
I'm using NUnit with AutoFixture, AutoMoq and the Theory attribute.
Here is my test method,
[TestFixture]
public class TestClass
{
[Theory, AutoMoqData]
public void TestI(I i)
{ }
}
the interface
public interface I
{ }
and the attribute
public class AutoMoqDataAttribute : AutoDataAttribute
{
public AutoMoqDataAttribute()
: base(new Fixture().Customize(new AutoMoqCustomization()))
{ }
}
When I build my solution the test is discovered. When I run the test the following is written to the Output window:
NUnit 1.0.0.0 executing tests is started
Run started: [...].Test.dll
NUnit 1.0.0.0 executing tests is finished
Test adapter sent back a result for an unknown test case. Ignoring result for 'TestI(Mock<TestClass+I:1393>.Object)'.
When using xUnit.net the above test is run correctly. Why is not it working with NUnit?
I have the following Nuget packages installed in the test project:
AutoFixture 3.18.1
AutoFixture.AutoMoq 3.18.1
Moq 4.2.1402.2112
NUnit 2.6.3
NUnitTestAdapter 1.0
I'm running the test from within Visual Studio 2013 Professional. I also tried running the test in the separate GUI runner, with the same result.
The following NUnit test passes in Visual Studio 2013 with the TestDriven.Net add-in:
internal class AutoMoqDataAttribute : AutoDataAttribute
{
internal AutoMoqDataAttribute()
: base(
new Fixture().Customize(
new AutoMoqCustomization()))
{
}
}
public interface IInterface
{
}
public class Tests
{
[Test, AutoMoqData]
public void IntroductoryTest(IInterface i)
{
Assert.NotNull(i);
}
}
The built-in test runner doesn't discover the above test. This looks like a bug in the test runner.