0

I use cake build system to automate building my solution and I want to automate testing my project with cake in our CI,

My NUnit projects have many types such as asp.net dot net framework 4.6.1 or dot net core 2.1 or ...

my problem is when I want to run our unit test with NUnit3 cake dsl we need to know dll path of each project, so I need to know what is type of each project, because for example for dot net core 2.1 dll is under netcoreapp directory and for another project dll is under somewhere else.

for better demonstration please see below image

Structure of my solution

As you can see in Test Solution Folder we have many test projects with different type framework and ... (for example one of them is dotnetcore2.1 another one is dot net framework 4.6.1 and etc.)

So my question is how to find type of project?

Is there any solution to NUnit3 found dll by self?

my GitHub discussion about this question

Dharman
  • 30,962
  • 25
  • 85
  • 135
sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53
  • Shouldn't that be a simple project reference from your test project to the teste projects? – Ackdari Feb 12 '21 at 13:49
  • No, each of my test projects has difference type – sorosh_sabz Feb 12 '21 at 13:54
  • Your setup is really unclear. For example, wouldn't you have one test-project for every project or at least every runtime-type? I mean you can't test a dotnet core and a .Net-Framework project in the same runtime. Also how are you configuring which projects should be tested in the first place? Please add information about the structure about your solution to the question. – Ackdari Feb 12 '21 at 14:04
  • I update my question to clear – sorosh_sabz Feb 12 '21 at 14:15
  • So the problem is to identify the runtime type of e.g. `*Commons.Tests` and not of `*.Commons`? – Ackdari Feb 12 '21 at 14:48
  • Yes I want to determine type of Commons.Tests in cake file, I prefer use builtin method and tools of Cake – sorosh_sabz Feb 12 '21 at 15:10

3 Answers3

1

You should use a project reference for your unit test projects as opposed to an explicit DLL reference.

When you're using .Net Core, Use XUnit testing for allow for near identical process for your test methods.

By using a project reference and having your test projects as part of the same Solution (sln) file, you'll greatly simplify your work.

Seeds
  • 372
  • 2
  • 14
  • I update my question to give more information and get clear meaning – sorosh_sabz Feb 12 '21 at 14:15
  • Why cant you use a test project that directly references the project within the solution? (TIL) NUnit has support for both .Net Core and .Net framework, you just need to have a respectively compiled solution type for each of them. – Seeds Feb 12 '21 at 18:45
  • NUnit3 Runner in cake does not support dot net core projects, so I have to use different runner for each project based on framework – sorosh_sabz Feb 13 '21 at 10:46
  • If you add the test runner projects as part of the solution, CAKE "should" have embedded support for handling the projects. However, i'm not an expert in CAKE. Sorry. I cannot assist with CAKE integration. :( This MAY assist with providing a direction towards your problem, but doesnt contain the entire solution. It lets you detect references, which may let you determine language type. https://stackoverflow.com/questions/46307971/xunit-working-with-net-core-using-cake – Seeds Feb 16 '21 at 16:25
1

It looks like what you are trying to do is to get a list of all the assemblies for your unit tests, across any targets that the project might have.

If that's the case, you could use the GetFiles alias to find the assemblies, and then inspect the FullPath of each assembly to check the TFM...

var files = GetFiles("./**/*.Tests.dll");
foreach(var file in files)
{
    Information("File: {0}", file.FullPath);

    if (file.FullPath.Contains("/net461/")
    {
        // ... Assembly is for .NET 4.6.1
    }
    else if (file.FullPath.Contains("/netcoreapp2.1/")
    {
        // ... Assembly is for .NET Core 2.1
    }
    // etc...
}
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • it may be best to do a Regex on the path such as "\//net[\d]*\/" for the .Net framework or "\/netcoreapp[\d]*\.[\d]*\/" to allow for multiple versions of each library. However this would apply an assertion of compatibility with your pipeline. Love the idea of using the generated path as opposed to reading the file. – Seeds Feb 16 '21 at 16:28
0

You could utilize a globber pattern to find all test projects, don't know you're exakt folder structure but you could do something like below

 Task("Test")
     .Does(() =>
 {
     var settings = new DotNetCoreTestSettings
     {
         Configuration = "Release"
     };

     var projectFiles = GetFiles("./**/Test/UnitTests/*.csproj");
     foreach(var file in projectFiles)
     {
         DotNetCoreTest(file.FullPath, settings);
     }
 });
devlead
  • 4,935
  • 15
  • 36
  • I cannot use DotNetCoreTest, Because for dot net framework I must use NUnit3 and for dotnetcore 2.1 I must use VSTest and for dotnetcore 3.1 I must use DotNetCoreTest, So I have to find a way to determine project framework type – sorosh_sabz Feb 12 '21 at 16:37