Reputation: 774
It would be great to have an attribute that can decorate a static method called AssemblyTearDown. It would mark the method that should be called after all the tests within the assembly have run to allow resources obtained by the assembly to be freed.
[AssemblyTearDown] - this is not supported in nunit 3.0
Is there any alternative in c# nunit which we can use instead of AssemblyTearDown. It would be called after all the other TearDown's have been processed
Upvotes: 1
Views: 218
Reputation: 3690
It took me some time to realize that outside any namespace means that no namespace should be present. Also I saw no difference if the method is static or not, the class and method names can be any valid names, here is the code that worked for me:
using NUnit.Framework;
[SetUpFixture]
public static class AssemblyCleanFixture
{
[OneTimeTearDown]
public static void RunOnAssemblyCleanUp()
{
//Your code here
}
}
Upvotes: 0
Reputation: 76
Despite this question being nearly a couple of years old, it seems it was left without an answer. After some research, I came across the answer to this today from NUnit's documentation for [SetUpFixture].
I think this should cover most bases when it comes to fulfilling the OP's requirements:
Essentially, if you decorate a class with the [SetUpFixture]
attribute outside of any namespace, and create a method within that class decorated with the [OneTimeTearDown]
attribute, this method will be executed once after all tests have completed execution.
Note that the [SetUpFixture]
attribute can also be used to provide one-time setup/teardown for a set of tests in a given namespace too, whereas, as per the OP's requirements, if the attribute is applied to a class outside of any namespace, the one-time setup and teardown methods (decorated with [OneTimeSetUp]
and [OneTimeTearDown]
respectively) provide setup/teardown for the entire assembly.
Example code from the documentation:
using System;
using NUnit.Framework;
namespace NUnit.Tests
{
[SetUpFixture]
public class MySetUpClass
{
[OneTimeSetUp]
public void RunBeforeAnyTests()
{
// ...
}
[OneTimeTearDown]
public void RunAfterAnyTests()
{
// ...
}
}
}
Hopefully, this is useful to somebody down the road!
Upvotes: 1