Repository navigation
Fix dotnet new nunit-test -n <name> creating an unwanted <name>/ folder - #54602
Merged
Evangelink merged 4 commits intoJun 7, 2026
Merged
Evangelink merged 4 commits into
Evangelink merged 4 commits into
Conversation
The NUnit item templates (C#, F#, VB) used 'preferNameDirectory: true',
which causes a folder to be created when -n is specified — this is the
correct behavior for project templates but wrong for item templates.
Replace with 'preferDefaultName: true' to match every other item
template in Microsoft.DotNet.Common.ItemTemplates (Class, Interface,
Struct, Enum, Record, Module, Structure, MSTest test classes). Also
align defaultName with sourceName ('UnitTest1') for consistency with
MSTest item templates.
Adds a regression test that creates a project then runs the item
template with -n (and no -o), asserting the file is created in the
working directory and no nested folder is created.
Fixes dotnet#49760
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a dotnet new item-template behavior regression where dotnet new nunit-test -n <name> run from inside an existing project directory incorrectly creates a nested <name>/ folder. It corrects the NUnit item template configuration to behave like other item templates in Microsoft.DotNet.Common.ItemTemplates and adds an integration test to prevent regressions.
Changes:
- Update the NUnit C#/F#/VB item templates to use
preferDefaultName: true(instead ofpreferNameDirectory: true) to avoid creating<name>/directories for item templates. - Align
defaultNamewithsourceName(UnitTest1) so default generation matches the shipped file name when-nisn’t provided. - Add a regression integration test ensuring
-nwithout-ocreates<name>.<ext>in the working directory and does not create a<name>/folder.
Show a summary per file
| File | Description |
|---|---|
| test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs | Adds a regression test covering item-template -n without -o from within a project directory. |
| template_feed/Microsoft.DotNet.Common.ItemTemplates/content/NUnit-CSharp-Item/.template.config/template.json | Switches NUnit C# item template to preferDefaultName and sets defaultName to UnitTest1. |
| template_feed/Microsoft.DotNet.Common.ItemTemplates/content/NUnit-FSharp-Item/.template.config/template.json | Switches NUnit F# item template to preferDefaultName and sets defaultName to UnitTest1. |
| template_feed/Microsoft.DotNet.Common.ItemTemplates/content/NUnit-VisualBasic-Item/.template.config/template.json | Switches NUnit VB item template to preferDefaultName and sets defaultName to UnitTest1. |
Copilot's findings
- Files reviewed: 4/4 changed files
- Comments generated: 0
Evangelink
enabled auto-merge
June 5, 2026 07:22
JanKrivanek
approved these changes
Jun 5, 2026
Evangelink
deleted the
dev/amauryleve/fix-49760-nunit-test-unwanted-folder
branch
June 7, 2026 10:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #49760
Problem
Root cause
The three NUnit item templates (C#, F#, VB) use
preferNameDirectory: true, which is the directive used by project templates (to create a folder named after the project). For item templates this is wrong: it nests the generated file inside a folder named after the item.Every other item template in
Microsoft.DotNet.Common.ItemTemplates(Class,Interface,Struct,Enum,Record,Module,Structure, and the MSTest test class templates) correctly usespreferDefaultName: trueinstead.Fix
preferNameDirectory: truewithpreferDefaultName: truein the three NUnit item templates.defaultNamewithsourceName(UnitTest1) so the no--ndefault matches the existing source file name, mirroring the MSTest item template pattern.ItemTemplate_WithNameAndNoOutput_DoesNotCreateUnwantedFolder) that creates a test project, then runs the item template with-n MyItemand the working directory set to the project directory (no-o), and asserts that:MyItem.<ext>exists in the project directoryMyItem/does not exist