C
C#2y ago
Hugh

✅ What is the standard way of doing tests? (and can I exclude tests from a library build?)

I was going to ask one question, but as I was told yesterday that I'm not following C# convention, I'm going to start by asking what the convention is for this! What is the standard convention for doing tests on a library? (I'm using NUnit) The way I've set it up so far is to have a "Tests" folder in the project, and running dotnet test is working well for that. However, I'm finding that the resulting .dll when I build the library includes the test classes, which I would like to avoid. Should I be doing the tests in a different way? Alternatively, when I call dotnet build, is there a way to tell it to exclude a folder so that it doesn't build the tests into the release? Thanks
22 Replies
Anton
Anton2y ago
the convention is to make a separate project for tests, or even for each project you want to test if you want to test internal things, you can use AssemblyVisibleTo
Hugh
HughOP2y ago
ah - okay - thanks - that's good to know So would it normally be structed as a single repository with 2 projects in?
Anton
Anton2y ago
yes
Hugh
HughOP2y ago
got it - thanks 🙂
Anton
Anton2y ago
np
Hugh
HughOP2y ago
And then in VSCode, my tasks.json could point at different project files for test and build Thanks - appreciate it
Anton
Anton2y ago
yeah you'd need 2 tasks
Hugh
HughOP2y ago
With a .csproj file, is it possible to have each one specify which folders to reference?
Anton
Anton2y ago
yes, but people usually don't do it
Hugh
HughOP2y ago
I'd love to keep both .csproj files in the root of the project, and have one look at the "Tests" folder, and the other at the "MyLib" folder ok so separate folders per project?
Anton
Anton2y ago
yes
Hugh
HughOP2y ago
Side question, then - do you know how, in vscode, I can tell the "test" task that it needs to run the "build" task first (to build the library)?
Anton
Anton2y ago
well there are tasks and there is the launch.json testing should probably go into launch
Hugh
HughOP2y ago
Ah - good point, as there's no executable to launch
Anton
Anton2y ago
But if you have multiple tasks and want to maintain them in the source control, vs code tasks are pretty fragile you move a folder or change the output directory and it will stop working
Hugh
HughOP2y ago
Right
Anton
Anton2y ago
for larger projects use rider there is the test dll
Hugh
HughOP2y ago
I meant for the library itself Yeah - I want to look at Rider I've used Rider for Unreal (for C++) a lot in the past, and really like it
Anton
Anton2y ago
executable and library are basically the same thing
Hugh
HughOP2y ago
okay. But the MyLib.dll doesn't have a Main in, so there's no point in launching it directly There we go. I've got my projects split out now
Anton
Anton2y ago
it's going to be loaded from the test dll
Hugh
HughOP2y ago
yes
Want results from more Discord servers?
Add your server