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
71
votes
3 answers

NUnit 3.0 and Assert.Throws

I am writing some unit tests with NUnit 3.0 and, unlike v2.x, ExpectedException() has been removed from the library. Based on this answer, I can definitely see the logic in trying to catch specifically where in the test one expects their system to…
Killnine
  • 5,728
  • 8
  • 39
  • 66
71
votes
2 answers

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 MSTest vs nUnit posts and articles in the past but most…
Null Reference
  • 11,260
  • 40
  • 107
  • 184
70
votes
3 answers

One-time initialization for NUnit

Where should I place code that should only run once (and not once per class)? An example for this would be a statement that initializes the database connection string. And I only need to run that once and I don't want to place a new method within…
code-ninja
  • 1,586
  • 1
  • 15
  • 23
70
votes
5 answers

NUnit3: Assert.Throws with async Task

I am trying to port a test to NUnit3 and am getting a System.ArgumentException : 'async void' methods are not supported, please use 'async Task' instead. [Test] public void InvalidUsername() { ... var exception =…
tikinoa
  • 1,267
  • 2
  • 10
  • 14
69
votes
5 answers

Code coverage with nUnit?

Is there a way to see the code coverage when using nUnit? I know there's such a feature in Visual Studio, but can you use it with nUnit or only with the built-in vs unit tests?
Lieven Cardoen
  • 25,140
  • 52
  • 153
  • 244
68
votes
9 answers

Unit test for thread safe-ness?

I've written a class and many unit test, but I did not make it thread safe. Now, I want to make the class thread safe, but to prove it and use TDD, I want to write some failing unit tests before I start refactoring. Any good way to do this? My…
TheSean
  • 4,516
  • 7
  • 40
  • 50
67
votes
3 answers

Setup Method With Params Array

I am developing tests for an application. There's a method that has a params array as a parameter. I have set up the method using Moq but when I run the test, the return value of the method is null, which means it is not being mocked. Here's a code…
Luis Aguilar
  • 4,331
  • 6
  • 36
  • 55
66
votes
4 answers

NUnit expected exceptions

I've got a set of test cases, some of which are expected to throw exceptions. Because of this, I have have set the attributes for these tests to expect exceptions like so: [ExpectedException("System.NullReferenceException")] When I run my tests…
Martin
  • 10,294
  • 11
  • 63
  • 83
65
votes
5 answers

Replace Console.WriteLine in NUnit

I haven't done much with NUnit before, but I just wanted to dump some text to a window in a console type fashion. For example: Console.WriteLine("... some information..."); That won't work of course because NUnit is driving things. I'm in the…
sgtz
  • 8,849
  • 9
  • 51
  • 91
65
votes
4 answers

How can I load this file into an NUnit Test?

I have the following IntegrationTest project structure ... If i wish to use that test data 126.txt in an NUnit Test, how do I load that plain txt file data? NOTE: The file is -linked- and I'm using c# (as noted by the image). cheers :)
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
65
votes
6 answers

How do I test database-related code with NUnit?

I want to write unit tests with NUnit that hit the database. I'd like to have the database in a consistent state for each test. I thought transactions would allow me to "undo" each test so I searched around and found several articles from 2004-05 on…
Michael Haren
  • 105,752
  • 40
  • 168
  • 205
65
votes
6 answers

Unit testing and checking private variable value

I am writing unit tests with C#, NUnit and Rhino Mocks. Here are the relevant parts of a class I am testing: public class ClassToBeTested { private IList insertItems = new List(); public bool OnSave(object entity, object…
TonE
  • 2,975
  • 5
  • 30
  • 50
62
votes
4 answers

How to do internal interfaces visible for Moq?

I have 3 projects in my C# solution. Signatures Structures Tests Signatures have public and internal interfaces. Also, it has [assembly: InternalsVisibleTo("Structures")] [assembly: InternalsVisibleTo("Tests")] in AssemblyInfo.cs…
Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47
62
votes
4 answers

How to run unit tests in STAThread mode?

I would like to test an app that uses the Clipboard (WindowsForms) and I need the Clipboard in my unit tests also. In order to use it, it should run in STA mode, but since the NUnit TestFixture does not have a main method, I don't know where/how to…
Peter
  • 47,963
  • 46
  • 132
  • 181
61
votes
4 answers

Moq: Setup a mocked method to fail on the first call, succeed on the second

What's the most succinct way to use Moq to mock a method that will throw an exception the first time it is called, then succeed the second time it is called?
anthony
  • 40,424
  • 5
  • 55
  • 128