9

Does NUnit support the concept of an Assembly Teardown similar to the Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute that can be applied to a static method?

This would be run after all the tests within the assembly have completed. I am currently using NUnit 2.6.0.

Rodney S. Foley
  • 10,190
  • 12
  • 48
  • 66

2 Answers2

9

It turns out this functionality does exist in NUnit it is just not very obvious or intuitive.

According to Charlie Poole in the feature request I made for this functionality here, he states the following will work, which is a direct quote from him.

Decorate a class outside of any namespace with [SetUpFixture]. Decorate a method of that class with [TearDown]. If you like, decorate another with [SetUp].

Differences from what you are asking for:

  1. The name makes it a little unobvious in this usage.

  2. NUnit allows any number of these and calls them all, without any guarantee of ordering. This is by design.

  3. It can be applied to a static or instance method. If it's an instance method, the class must have a default constructor and it will be created with a lifespan of the entire test run. This is also by design.

Now this is not pretty but it should achieve the same functionality, and maybe he will make it a little cleaner in 3.0. :)

Rodney S. Foley
  • 10,190
  • 12
  • 48
  • 66
  • As of NUnit 3, this throws an exception: `OneTimeSetUp: TearDownAttribute attribute not allowed in a SetUpFixture`, changing it to `[OneTimeTearDown]` fixes this. – MrLore Nov 16 '16 at 17:10
1

There is no such concept in NUnit. But you can apply TestFixtureTearDownAttribute to method to free resources obtained by TestFixture.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thanks that is what I feared, as there are unmanaged resources used by all TestFixtures that need to be cleaned up and when I ported a MSTest project to NUnit I hit this issue. – Rodney S. Foley Jan 11 '13 at 23:55
  • Created a feature request for this to be add to NUNit. If you are interested it can be found here: https://bugs.launchpad.net/nunitv2/+bug/1098766 – Rodney S. Foley Jan 12 '13 at 00:04