Is there a nunit setup attribute equivalent that could be added for some tests in xunit? - nunit

In NUnit when I had to do the setup for some tests I would use [Setup] Attribute.
Now in xUnit there are TextFixtures but they are only run before each test or run before all tests once, but there's no option to run it for some but not all tests.
This question is purely for my personal educational purposes.
Above can be easily solved by simply making a SetUp() method and just calling it inside the tests where it is needed, but I was wondering if there was a 'proper' way to do this.

Related

Is it possible to separate tests on "Nightly builds"

I am wondering if tests can be separated so that my whole test suite will not run on every build but runs on nightly builds.
Is it possible to separate it this way?
I know to separate the tests on multi browser but unsure in this case.
I did it by passing two seperate sets of tests. It worked! Thanks

How to make NUnit stop executing tests in a Class on a failure

Is there any way to make NUnit abort running any more tests in that class only when it encounters the first error? I'm running integration tests, using the [Order] attribute. These tests can get quite lengthy, and in certain cases there's no need to continue running tests in the class if one of the tests fail. I want NUnit to continue on to other classes, but I want it to abort calling any more classes in that specified class.
Is there any way to do this?
I'm using NUnit 3.2
Thanks!
Buzz
There isn't currently any way to stop execution of tests within a class on first error. There is only the command line option --stoponerror which stops running all tests on the first error.
The Order attribute is new in 3.2. Unlike other frameworks, it was only intended to order your tests, not setup dependencies for your tests. There is an open enhancement on GitHub for a Test Dependency Attribute, but it hasn't been worked on because people cannot come to a consensus on the design. I think a good first step would be a variation of the Order attribute with dependencies, but some people want a full dependency graph. I would recommend that you head over to GitHub and comment on the issue with your requirements.

How do i profile the performance of nunit tests that are run through resharper?

I'm using resharper to run my nunit tests - and i'd like to improve performance
I know that resharper uses a built in version of nunit. How do i set up resharper/nunit, such that i can run my unit tests through a profiler and see where i can best spend my time when optimizing?
Actually, with dotTrace you can profile Nunit tests right from Visual Studio or Rider, you don't need to use attaching: https://www.jetbrains.com/help/profiler/Profiling_Guidelines__Profiling_Unit_Tests.html
Please note that dotTrace is a part of ReSharper Ultimate package, you need to have the corresponding license.
I profile my code with dotTrace. You can start your tests and profile it with dotTrace (I start the test and attach the dotTrace to it). Second option is to use StopWatch. I would put StopWatch lines that I want to profile and print the values and depending on the results, I would start analyzing the code.
Stopwatch sw = Stopwatch.StartNew();
// code that you want to measure
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
These are the things that comes to my mind.

Show only specific Tests or TestFixtures in NUNIT via a configuration file or another way

I have a bunch of NUNIT tests in several TestFixtures. Currently, I just display all the tests for everyone. Is there a way to hide some tests and/or test fixtures. I have various "customers" and they don't all need to see every test. For example, I have engineers using low level tests, and I have a QA department that is using higher level tests. If I could have a configuration (XML?) file that I distributed with the dll that would be ideal. Can someone point me to the documentation and example? I did search the NUNIT site and did not see anything.
I am aware of the [IGNORE] attribute and I suppose a somewhat acceptable solution would be to have a configuration file that can apply IGNORE to various tests or testfixtures. I'd hand out a different version of the configuration file to each customer. At least that way certain customers would not be able run certain tests.
I'm using version 2.5.5
Ideas?
Thanks,
Dave
Yes - if the tests are in seperate assemblies, this can be accomplished by proper configuration of your NUnit projects. However, this is not an option if the tests are in one large test assembly. If this is the case, you may wish to break up the test assembly. Here is the documentation on the NUnit ProjectEditor: http://www.nunit.org/index.php?p=projectEditor&r=2.2.10

xUnit framework: Equivalent of [TestFixtureSetUp] of NUnit in XUnit?

What is xUnit's equivalent of NUnit's [TestFixtureSetUp]?
We have explored and found that IUseFixture<T> is the equivalent of [TestFixtureSetUp], but it's not working as expected.
As we have explored (in case of NUnit), we found that [TestFixtureSetUp] marked methods for code to be executed only once before all test in the fixture have been run. In xUnit, the equivalent of [TestFixtureSetUp] is IUseFixture<T> as we have explored, but during testing, we found that the SetFixture method of IUseFixture is being called before every test (not only once for all methods).
Please let us know how can we achieve the above in xUnit. Also correct us if we are misunderstanding something. Thanks.
There is no direct equivalent of [TestFixtureSetUp] in XUnit, but you can achieve similar functionality. This page lays out the translation between NUnit and XUnit (as well as a couple other C#/.NET test frameworks). However, XUnit largely got rid of setups/teardowns (this article explains why that decision was made). Instead, you need the test suite to implement an interface called IUseFixture<T> which can initialize some data for the fixture.
You might also want to read this overview of XUnit, written from the perspective somebody coming from an NUnit/MbUnit background.