I've just started to work with NUnit in order to provide some test coverage for my projects.
Within my main library.dll I need to load up configuration data from an external file that goes with the library, library.xml.
This works fine when I'm using the library, because I use the following to get the directory in which to look for the config file:
string settingspath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
The problem I've noticed is that when I'm unit testing with NUnit, it copies my assemblies to a Shadow Copy, but doesn't take any of the other files with it, so of course my init fails due to missing config files.
Should I be doing something different to locate config files from within my library? (it's a server app, and I don't want to use the standard app settings, or user's local settings, etc)
There are three different test frameworks that are supported by ASP.NET Core for unit testing - MSTest, xUnit, and NUnit. These allow us to test our code in a consistent way.
NUnit 3.7. The AssertionHelper class has now been deprecated.
There is no facility in NUnit to order tests globally. Tests with an OrderAttribute argument are started before any tests without the attribute. Ordered tests are started in ascending order of the order argument. Among tests with the same order value or without the attribute, execution order is indeterminate.
Navigate to Tools -> NuGet Package Manager -> Manager NuGet Packages for Solution. Search for NUnit & NUnit Test Adapter in the Browse tab. Click on Install and press OK to confirm the installation. With this installation, the NUnit framework can be used with C#.
Use can use TestContext.CurrentContext.TestDirectory
as mentioned by Charlie Poole from NUnit here:
The need to access files in the same directory as the test assembly is the most commonly cited reason for disabling shadow copy. However, this is spurious.
NUnit doesn't copy any assemblies: shadow copy is a function of .NET itself. Consequently, the problem needs to be viewed as "How can I access the file where it is?" rather than "How can I get the file copied to where I think it should be?"
There are three ways to locate a file that is located in the same directory as the assembly:
1) Use Assembly.Codebase - this will give you the location as a URI, which you must then tranform to an appropriate path.
2) Use the current directory, which NUnit has historically set to directory containing the executing test assembly. However, this may not be true for future releases.
3) Use NUnit's TestContext.CurrentContext.TestDirectory, which is the available in the most recent releases.
All of these approaches actually use Assembly.Codebase under the covers, with NUnit doing the work of tranforming the URI correctly in #2 and #3. The common approach of using Assembly.Location is incorrect, unless you actually want the location of the shadow copy cache.
For using reference files in my Unit Tests, I use Assembly.Codebase which works even if Shadow Copying is turned on. You might want to give it a try..
It returns a string in the Uri format.. so you'd need to create a Uri instance from the codebase string and use Uri.LocalPath to get the actual folder path.
However for production code, the BaseFolder should be retrieved from a well known place (e.g. a Registry key set via the installer on Windows). All file lookups should be rooted from this baseFolder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With