Repository navigation
Add MSTest DeploymentItem sub-page #54210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Evangelink
merged 4 commits into
dotnet:main
from
Evangelink:dev/amauryleve/mstest-deployment-items
Jun 22, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
338e288
Add MSTest DeploymentItem sub-page
Evangelink 71f41b7
Fix DeploymentItem snippet compile issues and rename heading
Evangelink 37ff122
Apply suggestions from code review
Evangelink 358fcea
Fix bookmark-not-found: correct intra-page anchors for constructor he…
Evangelink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
docs/core/testing/unit-testing-mstest-writing-tests-deployment-items.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| --- | ||
| title: Deploy files alongside MSTest tests | ||
| description: Learn how to use the DeploymentItemAttribute in MSTest to copy files and folders alongside test assemblies for use during test execution. | ||
| author: Evangelink | ||
| ms.author: amauryleve | ||
| ms.date: 06/09/2026 | ||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # Deploy files alongside MSTest tests | ||
|
|
||
| Some tests need extra files at run time, such as test data, configuration files, golden masters, or native dependencies. Use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute> to declare files and folders that should be available next to your test assembly when each test runs. | ||
|
|
||
| ## Overview | ||
|
|
||
| When you apply `[DeploymentItem]` to a test class or a test method, MSTest copies the specified files or folders into the directory exposed by <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DeploymentDirectory?displayProperty=nameWithType> before any test in that scope runs. The deployment directory is also the current working directory for the test, so your test code can open the files by their copied names. | ||
|
|
||
| The attribute accepts a relative or absolute path: | ||
|
|
||
| - **Relative paths** are resolved against the build output directory (the folder that contains the test assembly, for example `bin\Debug\net10.0\`). | ||
| - **Absolute paths** are used as-is. | ||
|
|
||
| > [!IMPORTANT] | ||
| > In MSTest 3.x, deployment items are copied per test run. To make a file available at deployment time, the file must already exist in (or be copied to) the build output directory. | ||
|
|
||
| ## Apply `[DeploymentItem]` | ||
|
|
||
| The attribute can be applied to a test method, a test class, or both. Multiple instances are allowed and combine: | ||
|
|
||
| ```csharp | ||
| using System.IO; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [TestClass] | ||
| [DeploymentItem(@"TestFiles\shared-config.json")] | ||
| public class ConfigurationTests | ||
| { | ||
| [TestMethod] | ||
| [DeploymentItem(@"TestFiles\customers.csv")] | ||
| public void LoadCustomers_FromCsv_ReturnsAllRows() | ||
| { | ||
| // Both shared-config.json (from the class) and customers.csv (from | ||
| // the method) are available in the deployment directory. | ||
| Assert.IsTrue(File.Exists("shared-config.json")); | ||
| Assert.IsTrue(File.Exists("customers.csv")); | ||
|
Evangelink marked this conversation as resolved.
|
||
| } | ||
| } | ||
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > When you apply `[DeploymentItem]` to a test class, the class must contain at least one test method. Applying it to a class that only has `AssemblyInitialize` or `ClassInitialize` methods has no effect. Analyzer [MSTEST0035](mstest-analyzers/mstest0035.md) flags such misuse. | ||
|
|
||
| ## Constructor overloads | ||
|
|
||
| `DeploymentItemAttribute` has two constructors. | ||
|
Evangelink marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### `DeploymentItemAttribute(string path)` | ||
|
|
||
| Copies the file or folder identified by `path` into the root of the deployment directory. | ||
|
|
||
| ```csharp | ||
| // Copy a single file from the build output directory. | ||
| [DeploymentItem("settings.json")] | ||
|
|
||
| // Copy a file that lives in a subfolder of the build output directory. | ||
| // The file is copied to the root of the deployment directory (the | ||
| // "Resources" folder is not preserved). | ||
| [DeploymentItem(@"Resources\test-data.xml")] | ||
|
|
||
| // Copy the entire TestFiles folder (and all of its subfolders) into the | ||
| // deployment directory. | ||
| [DeploymentItem("TestFiles")] | ||
| ``` | ||
|
|
||
| ### `DeploymentItemAttribute(string path, string outputDirectory)` | ||
|
|
||
| Copies the items into a subdirectory of the deployment directory, given by `outputDirectory`. | ||
|
|
||
| ```csharp | ||
| // Creates a "Data" subfolder under the deployment directory, then copies | ||
| // test-data.xml into it. The file is reached at "Data\test-data.xml". | ||
| [DeploymentItem("test-data.xml", "Data")] | ||
|
|
||
| // Copies the contents of the Resources folder into a "Resources" | ||
| // subfolder of the deployment directory. | ||
| [DeploymentItem("Resources", "Resources")] | ||
| ``` | ||
|
|
||
| The `outputDirectory` parameter must be a folder path. It can't be used to rename the file. To deploy a file with a different name, rename it in the source folder (or use a post-build step). | ||
|
Evangelink marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Ensure source files reach the build output directory | ||
|
|
||
| Because relative paths are resolved against the build output directory, the source file or folder must already be there. There are two common ways to achieve this. | ||
|
|
||
| ### Use `<None>` or `<Content>` with `CopyToOutputDirectory` | ||
|
|
||
| Add the files to your test project and mark them to copy to the build output directory: | ||
|
|
||
| ```xml | ||
| <ItemGroup> | ||
| <None Update="TestFiles\**\*.*"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| After a build, the `TestFiles` folder is replicated in `bin\<Configuration>\<TargetFramework>\TestFiles\`, and `[DeploymentItem("TestFiles")]` resolves correctly. | ||
|
|
||
| ### Use a post-build target | ||
|
|
||
| For files that live outside the test project, copy them into the build output directory as part of the build: | ||
|
|
||
| ```xml | ||
| <Target Name="CopySharedAssets" AfterTargets="Build"> | ||
| <Copy SourceFiles="@(SharedAsset)" | ||
| DestinationFolder="$(OutDir)SharedAssets\" /> | ||
| </Target> | ||
| ``` | ||
|
|
||
| ## Inspect the deployment directory at run time | ||
|
Evangelink marked this conversation as resolved.
Outdated
|
||
|
|
||
| Use <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DeploymentDirectory?displayProperty=nameWithType> if you need the absolute path of the deployment directory—for example, to pass it to a process you spawn or to log it for diagnostics: | ||
|
Evangelink marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```csharp | ||
| using System.IO; | ||
|
|
||
| [TestMethod] | ||
| [DeploymentItem(@"TestFiles\input.json")] | ||
| public void ProcessInput_FromDeployedFile_Succeeds() | ||
| { | ||
| string fullPath = Path.Combine(TestContext.DeploymentDirectory, "input.json"); | ||
| string contents = File.ReadAllText(fullPath); | ||
|
Evangelink marked this conversation as resolved.
|
||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| For more on `TestContext`, see [The `TestContext` class](unit-testing-mstest-writing-tests-testcontext.md). | ||
|
|
||
| ## When deployment doesn't happen | ||
|
|
||
| By default, MSTest creates a per-run deployment directory and copies items into it. You can disable deployment in a `.runsettings` file so tests run directly out of the build output directory: | ||
|
|
||
| ```xml | ||
| <RunSettings> | ||
| <MSTest> | ||
| <DeploymentEnabled>False</DeploymentEnabled> | ||
| </MSTest> | ||
| </RunSettings> | ||
| ``` | ||
|
|
||
| When deployment is disabled, `[DeploymentItem]` attributes have no effect, and the test runs in the build output directory itself. For more configuration options, see [Configure MSTest](unit-testing-mstest-configure.md). | ||
|
|
||
| ## Legacy mode and `.testsettings` | ||
|
|
||
| When MSTest runs in legacy mode (a `.testsettings` file is used, or `RunSettings/MSTest/ForcedLegacyMode` is set to `true` in a `.runsettings` file), relative paths might be resolved against the solution root directory instead of the build output directory. Avoid legacy mode for new projects—the modern `.runsettings`-based configuration is the recommended approach. | ||
|
|
||
| ## Best practices | ||
|
|
||
| - **Prefer `CopyToOutputDirectory` over deep relative paths.** Don't reach into source folders with `..\..\` style paths—they tie your tests to a specific repository layout. Stage the files in the build output directory first. | ||
| - **Keep deployment items small.** Each item is copied for every test run; large files slow down test execution. | ||
| - **Use folders to deploy related assets together.** `[DeploymentItem("TestFiles")]` is easier to maintain than dozens of per-file attributes. | ||
| - **Prefer embedded resources or in-memory data for small fixtures.** Embedded resources eliminate the need for deployment and avoid I/O at test time. | ||
| - **Don't rely on the working directory being the project directory.** During test execution, the working directory is the deployment directory, not the test project folder. | ||
|
|
||
| ## See also | ||
|
|
||
| - <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute> | ||
| - <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DeploymentDirectory?displayProperty=nameWithType> | ||
| - [The `TestContext` class](unit-testing-mstest-writing-tests-testcontext.md) | ||
| - [MSTEST0035: `[DeploymentItem]` can be specified only on test class or test method](mstest-analyzers/mstest0035.md) | ||
| - [Configure MSTest](unit-testing-mstest-configure.md) | ||
| - [Write tests with MSTest](unit-testing-mstest-writing-tests.md) | ||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.