Questions tagged [nunit]

NUnit is an open source unit testing framework for .NET and Silverlight written in C#. It serves the same purpose as JUnit or TestNG does in the Java world, and is one of many in the xUnit family.

Initially ported from JUnit, this xUnit based unit testing tool for was written entirely in and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages. The current production release, version 3, has been completely rewritten with many new features and support for a wide range of .NET platforms.

References

Tutorials

7425 questions
412
votes
7 answers

NUnit vs. MbUnit vs. MSTest vs. xUnit.net

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? Does it matter? Which one is most future proof…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
401
votes
9 answers

Different return values the first and second time with Moq

I have a test like this: [TestCase("~/page/myaction")] public void Page_With_Custom_Action(string path) { // Arrange var pathData = new Mock(); var pageModel = new Mock(); var repository…
marcus
  • 9,616
  • 9
  • 58
  • 108
311
votes
9 answers

How do I use Assert.Throws to assert the type of the exception?

How do I use Assert.Throws to assert the type of the exception and the actual message wording? Something like this: Assert.Throws( ()=>user.MakeUserActive()).WithMessage("Actual exception message") The method I am testing throws…
epitka
  • 17,275
  • 20
  • 88
  • 141
256
votes
18 answers

NUnit vs. Visual Studio 2008's test projects for unit testing

I am going to be starting up a new project at work and want to get into unit testing. We will be using Visual Studio 2008, C#, and the ASP.NET MVC stuff. I am looking at using either NUnit or the built-in test projects that Visual Studio 2008 has,…
Ryan Skarin
  • 3,087
  • 3
  • 21
  • 18
256
votes
5 answers

Verifying a specific parameter with Moq

public void SubmitMessagesToQueue_OneMessage_SubmitSuccessfully() { var messageServiceClientMock = new Mock(); var queueableMessage = CreateSingleQueueableMessage(); var message = queueableMessage[0]; var xml =…
Luis Mirabal
  • 2,606
  • 2
  • 16
  • 12
187
votes
8 answers

How to find path of active app.config file?

I'm trying to finish this exception handler: if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null) { string pathOfActiveConfigFile = ...? throw new ConfigurationErrorsException( "You either forgot to set the connection…
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
183
votes
10 answers

NUnit vs. xUnit

What are the differences between NUnit and xUnit.net? What's the point of developing two of them, not only one? I've read that xUnit is being developed by inventor of NUnit: xUnit.net is a unit testing tool for the .NET Framework. Written by the…
Ruslan
  • 2,678
  • 3
  • 22
  • 25
183
votes
5 answers

Test Explorer (VS) shows ''

Everthing below is made in VS2019, using .NET Framework 4.7 and NUnit + NUnit3TestAdapter I created an assembly called Exitus.Tests, and added a few unit tests. However, do to some issues with Nuget, that I could not solve, I made another project…
Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
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
9 answers

How can I run NUnit tests in Visual Studio 2017?

I've just installed Visual Studio 2017. I have a project using NUnit for the test cases. Ctrl + R - T no longer runs the tests, and the Test Explorer no longer finds any test cases marked with the TestCase attribute. Is there a way to get NUnit…
user2286552
169
votes
5 answers

Use NUnit Assert.Throws method or ExpectedException attribute?

I have discovered that these seem to be the two main ways of testing for exceptions: Assert.Throws(()=>MethodThatThrows()); [ExpectedException(typeof(Exception))] Which of these would be best? Does one offer advantages over the other?…
SamuelDavis
  • 3,312
  • 3
  • 17
  • 19
157
votes
6 answers

'await' works, but calling task.Result hangs/deadlocks

I have the following four tests and the last one hangs when I run it. Why does this happen: [Test] public void CheckOnceResultTest() { Assert.IsTrue(CheckStatus().Result); } [Test] public async void CheckOnceAwaitTest() { …
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
156
votes
20 answers

Compare equality between two objects in NUnit

I'm trying to assert that one object is "equal" to another object. The objects are just instances of a class with a bunch of public properties. Is there an easy way to have NUnit assert equality based on the properties? This is my current solution…
Michael Haren
  • 105,752
  • 40
  • 168
  • 205
151
votes
26 answers

NUnit Unit tests not showing in Test Explorer with Test Adapter installed

I've installed NUnit Test Adapter for VS2012 + 2013. When I first installed the Adapter tests were showing up, but they stopped showing up for some reason today. After building, rebuilding, cleaning, restarting, nothing shows up in Test Explorer. …
RobVious
  • 12,685
  • 25
  • 99
  • 181
149
votes
6 answers

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] public class StringFormatUtilsTest { …
tjjjohnson
  • 3,270
  • 4
  • 31
  • 33
1
2 3
99 100