Xunit test not copying appsettings file to output directory - .net-core

I have a Xunit test project that targets both .NET Core 3.1 and .NET Framework 4.6.1. My test loads an appsettings.json file as detailed by Rick Strahl. When the test runs, even though I have set the file properties to be of Content type and to always copy to output directory, the file isn't actually there when the test runs.
Now I was successfully able to use this technique in another unit test project, but the difference is that one targets .NET Core 3.1 only. In the working test project, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) is C:\git\myproject\src\myproject.tests\bin\Debug\netcoreapp3.1 (i.e., the build output directory). However, in the failing test, the location is C:\Users\hcheng\AppData\Local\Temp\ffe98278-56d3-41f6-8864-e50c74cb2c08\ffe98278-56d3-41f6-8864-e50c74cb2c08\assembly\dl3\8ba8de87\6b2a96ca_54b8d601 and when I open Windows Explorer to that path, the only contents in that folder are the DLL and the PDB of the unit test project, and __AssemblyInfo__.ini (none of the other dependencies are there either).
I'm totally new to .NET Core so I can't really tell what I'm doing wrong, but I suspect that the multi-targeting is related. Since the other dependencies are missing from that temp location, I'm also worried that if I get past this hurdle of loading the config file, I'll still get TypeLoadExceptions afterwards.
Thanks in advance for your help.

I eventually found the answer in How to refer to test files from Xunit tests in Visual Studio?.
Instead of Assembly.GetExecutingAssembly().Location, I had to use new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath.

Related

Disable parallel execution of "Clean solution" in .NET Core

We have a solution in .NET Core 3.1 with multiple project. All project have same build output.
This worked fine until recently a build start failing on all machines. (Some update?)
Build works
Rebuild fails.
Clean + Rebuild works.
I can reproduce the issue in Visual Studio and in the Rider as well.
The root cause is following:
Rebuild start building every project in parallel as separate task. Each task first delete output folder and then build a project.
Because all project have same output folder and run in parallel, they just delete files created by another project build which result into error:
Microsoft.Common.CurrentVersion.targets(4919, 5): [MSB3030] Could not copy the file "C:\myproject\x64\Debug\Project1.deps.json" because it was not found.
Microsoft.Common.CurrentVersion.targets(4919, 5): [MSB3030] Could not copy the file "C:\myproject\x64\Debug\Project1.runtimeconfig.json" because it was not found.
Microsoft.Common.CurrentVersion.targets(4919, 5): [MSB3030] Could not copy the file "C:\myproject\x64\Debug\Project1.runtimeconfig.dev.json" because it was not found.
Obviously a simple solution would be to do separate output folder, but I cannot do this because another tools expect this structure and because it is kind of rule.
I would like to clean the output on rebuild first before each project clean+build is trigger.
Another solution I can imagine is to generate deps.json, runtimeconfigs files into separate folder.
Is there any solution for this?
Maybe you Can set project dependencies in solution properties... that way they should be forced to build one after the other. eg. A depends on B , B on C
You should probably not change the build output paths, simply do a publish?

dotnet build CS2012 cannot open dll for writing

I have a .Net Core 3.1 DLL project that all of a sudden didn't build anymore with the following error message:
CSC : error CS2012: Cannot open 'some.dll' for writing -- 'Access to the path 'some.dll' is denied.'
This project is not under any kind of source control. It's not on a build server (what most questions on Stack Overflow is about). It's a project on my local machine.
Now, to rule out Visual Studio I've tried to build it from the command line with dotnet. Same thing unfortunately. Things I've tried:
Close Visual Studio, delete obj folder + bin folder
Delete the entire project and made a new one with the same name
Removed project reference to other project (to rule out: dotnet core build in parallel or simultaneously)
The suggestions here error CS2012: Cannot open <executable path> access to <executable path denied>
This all didn't help. Then I had success for 1 build with:
Changing the target framework from 3.1 to 3.0
Changing the name of the project
This built the project once, because the filepath changes really, but then I got the same error the 2nd built.
Then I've had a few days success by:
Moving the entire solution to a new folder without the problematic project. Then adding a brand new project and adding the code to this complete new project.
But unfortunately after a few days I got the exact same problem. I have no idea what changed. It's always this project (which is a unittest project) and not the other project that it references (also a DLL project). I am out of ideas. Anybody have any suggestions for me to try?
Thanks in advance for helping.
Update
My project's name = "TheGenesysProject.Engine.Test" and it was indeed quarantined by my company's security software as Pavel said in the comments. So I changed it to "JustSomeLib" and the security software didn't quarantine it anymore! Why this is, I have no clue whatsoever...
Update 2
it must be something in the project itself and not the name of the dll. I restored JustSomeLib so it had the same NuGet packages (xunit + xunit.runner.visualstudio) plus .cs files (just 3 files with some unittests in them nothing fancy) as TheGenesysProject.Engine.Test and it all build and worked once! Then I coded some more stuff. Added an unittest to test my new code and... Bam! In quarantine again. What the heck?! I am just logging this for if people have the same problem as I do.
Update 3
Just to conclude this story. The folder with sourcecode is now excluded from the malware scan and the dll's are not put in quarantine. This solved this problem. Thank you Pavel.

What's the way of running NUnit tests on .Net Core project which refers .NetStandard project including those tests?

I have my tests defined in .NetStandard project (assembly) (as I need to test them through very different platforms).
We can't run those directly as it requires the project to target something like .NetFramework or .Net Core.
So, I've created .Net Core project and referencing my .NetStandard project.
However, when running dotnet test ...csproj I obviously receive
No test is available in .... Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
... as there is obviously no magic and testing adapter can't read my mind and go to any referenced assembly automatically.
That seems that the tests discoverer just doesn't look at the attached assemblies with tests.
However, there must be some way of mentioning that to it, right?
The documentation is quite modest about that, so I don't even know if this approach in general possible.
However, when testing Xamarin.iOS/Android with TestRunners, they always have the option to add my assembly to test, like this: runner.Add(typeof(MyTestClass).Assembly).
Just to be clear, that this question point is not actually about targeting different platforms issue, but finding the way of running tests from .Net Core project (the way it already works for Xamarin):
One of the solutions for this is creating an inherited class in .NET Core project from the class(es) of .NETStandard project:
[TestFixture]
public class TestsClassInNetCoreProject : TestsClassInNetStandardProject
{
// ...
}
(it would work both ways: either from command line or from VS "Run Tests" menu) but this is of course not the best way of handling this.
Just to summarize:
The tests should be placed in separate project/assembly as for Xamarin test projects they don't actually run in common way tests run (they run via a test runner inside of actually an iOS/Android app).
This way it's handy to have the tests in .NETStandard assembly, which can be attached anywhere else.
Again, this already works for Xamarin (platform-specific project attaching my .NETStandard library with tests) via a test runner (NUnit one), but it doesn't for .NET Core project.
I don't need to run the tests on the .NET Standard assembly itself (what is even not possible), I need they to be referenced from testable .NET Core project.
Is there any way I can be lucky with that?

TeamCity won't build with NUnit class library

I have a .NET solution set up that has the main ASP MVC project and then a class library project that has had NUnit added to it for unit tests.
When I build the solution in TeamCity the main project builds fine but then when the test project builds I get these errors
error BC30002: Type 'TestFixture' is not defined.
error BC30002: Type 'Test' is not defined.
error BC30389: 'System.Diagnostics.Assert' is not accessible in this context because it is 'Friend'.
I assume it can't find the NUnit.dll or something but the dll is in the solution packages folder. I've tried doing package restore first in case it was that but it still did the same.
Update
Ok, just to test it I added Moq to the project to see if it would pick up the references. It did and worked fine, it seems to be only problem when I try to use NUnit functions. I could disable it from building but surely the NUnit tests won't be picked up in Team City?
Thanks!
Finally fixed the issue and it was my own stupid mistake. Thanks to DevOps for suggesting going through the MSBuild logs, it helped my track down the problem.
The actual DLL wasn't getting checked in to source control because the dev machine that added the test project had a global ignore file to ignore all DLL's. So I thought it was there but it was just the .xml file and I mis read it.
Anyway it now builds and runs the tests.
Thanks for the help everyone

Adding NUnit to the options for ASP.NET MVC test framework

I have nUnit installed.
I have VS2008 Team Edition installed.
I have ASP.Net MVC Preview 4 (Codeplex) installed.
How do I make Visual Studio show me nUnit as a testing framework when creating a new MVC project? At this point I still only have the Microsoft Testing Framework as a choice.
Update: I installed nUnit 2.5, but still with no success. From what I've found Googling, it would seem I need to create templates for the test projects in order for them to be displayed in the "Create Unit Test Project". I would have thought that templates be readily available for nUnit, xUnit, MBUnit, et. al. Also, it looks like I need to created registry entries. Anybody have any additional information?
Update: I determined the answer to this through research and it's posted below.
After a bunch of research and experimentation, I've found the answer.
For the record, the current release of nUnit 2.5 Alpha does not seem to contain templates for test projects in Visual Studio 2008.
I followed the directions here which describe how to create your own project templates and then add appropriate registry entries that allow your templates to appear in the drop-down box in the Create Unit Test Project dialog box of an MVC project.
From a high level, what you have to do is:
Create a project
Export it as a template (which results in a single ZIP archive)
Copy it from the local user's template folder to the Visual Studio main template test folder
Execute devenv.exe /setup
Run regedit and create a few registry entries.
So much for the testing framework selection being easy! Although, to be fair MVC is not even beta yet.
After all that, I did get the framework of choice (NUnit) to show up in the drop down box. However, there was still a bit left to be desired:
Although the test project gets properly created, it did not automatically have a project reference to the main MVC project. When using Visual Studio Unit Test as the test project, it automatically does this.
I tried to open the ZIP file produced and edit the MyTemplate.vssettings file as well as the .csproj project file in order to correct the aforementioned issue as well as tweak the names of things so they'd appear more user friendly. This for some reason does not work. The ZIP file produced can not be updated via WinZip or Win-Rar -- each indicates the archive is corrupt. Each can extract the contents, though. So, I tried updating the extracted files and then recreating the ZIP file. Visual Studio did not like it.
So, I should probably read this as well which discusses making project templates for Visual Studio (also referenced in the blog post I linked to above.) I admit to being disappointed though; from all the talk about MVC playing well with other testing frameworks, etc, I thought that it'd be easier to register a 3rd party framework.
Man, they have VS 2008 project template listed in their release notes. I guess that doesn't mean they have it integrated with the dialog yet.
I use MbUnit with Gallio and everything worked like a charm. I had to install an Alpha of Gallio and MbUnit and when I read the above in the release notes, I figured they implemented it also.
Just keep a look out on nUnit's site for future alpha releases. I am sure they'll have it implemented soon. You could also implement the feature yourself and submit a patch. :-)
Although they do not have one bundled with the framework here is a link to post containing a download to automatically create the test project for "NUnit with moq" for you NUnit with Moq
(did not work right away on my computer, W7 Beta, make sure you use elevated permissions)
Do install Testdriven.net to integrate NUnit with Visual Studio. MbUnit and later versions of NUnit also contain project templates for unit tests.
You can use those project templates to create a test project and then reference to your ASP.NET MVC project and be able to test its code.

Resources