{"id":31625,"date":"2022-10-18T18:37:17","date_gmt":"2022-10-18T18:37:17","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31625"},"modified":"2022-12-23T05:45:48","modified_gmt":"2022-12-23T05:45:48","slug":"dotnet-test-command-reference","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/dotnet-test-command-reference\/","title":{"rendered":"dotnet test command reference"},"content":{"rendered":"\n<p>The&nbsp;<code>dotnet test<\/code>&nbsp;command is used to execute unit tests in a given solution. The&nbsp;<code>dotnet test<\/code>&nbsp;command builds the solution and runs a test host application for each test project in the solution. The test host executes tests in the given project using a test framework, for example: MSTest, NUnit, or xUnit, and reports the success or failure of each test. If all tests are successful, the test runner returns 0 as an exit code; otherwise if any test fails, it returns 1.<\/p>\n\n\n\n<p>For multi-targeted projects, tests are run for each targeted framework. The test host and the unit test framework are packaged as NuGet packages and are restored as ordinary dependencies for the project.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Examples\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory:\ndotnet test\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the test1 project:\n$ dotnet test ~<span class=\"hljs-regexp\">\/projects\/<\/span>test1\/test1.csproj\n\nRun the tests using test1.dll assembly:\n$ dotnet test ~<span class=\"hljs-regexp\">\/projects\/<\/span>test1\/bin\/debug\/test1.dll\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and generate a test results file <span class=\"hljs-keyword\">in<\/span> the trx format:\n$ dotnet test --logger trx\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and generate a code coverage file (after installing Coverlet collectors integration):\n$ dotnet test --collect:<span class=\"hljs-string\">\"XPlat Code Coverage\"<\/span>\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and generate a code coverage file (Windows only):\n$ dotnet test --collect <span class=\"hljs-string\">\"Code Coverage\"<\/span>\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and log <span class=\"hljs-keyword\">with<\/span> detailed verbosity to the <span class=\"hljs-built_in\">console<\/span>:\n$ dotnet test --logger <span class=\"hljs-string\">\"console;verbosity=detailed\"<\/span>\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and log <span class=\"hljs-keyword\">with<\/span> the trx logger to testResults.trx <span class=\"hljs-keyword\">in<\/span> the TestResults folder:\n$ dotnet test --logger <span class=\"hljs-string\">\"trx;logfilename=testResults.trx\"<\/span>\n\nSince the log file name is specified, the same name is used <span class=\"hljs-keyword\">for<\/span> each target framework <span class=\"hljs-keyword\">in<\/span> the <span class=\"hljs-keyword\">case<\/span> <span class=\"hljs-keyword\">of<\/span> a multi-targeted project. The output <span class=\"hljs-keyword\">for<\/span> each target framework overwrites the output <span class=\"hljs-keyword\">for<\/span> preceding target frameworks. The file is created <span class=\"hljs-keyword\">in<\/span> the TestResults folder <span class=\"hljs-keyword\">in<\/span> the test project folder, because relative paths are relative to that folder. The following example shows how to produce a separate file <span class=\"hljs-keyword\">for<\/span> each target framework.\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and log <span class=\"hljs-keyword\">with<\/span> the trx logger to files <span class=\"hljs-keyword\">in<\/span> the TestResults folder, <span class=\"hljs-keyword\">with<\/span> file names that are unique <span class=\"hljs-keyword\">for<\/span> each target framework:\n\n$ dotnet test --logger:<span class=\"hljs-string\">\"trx;LogFilePrefix=testResults\"<\/span>\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and log <span class=\"hljs-keyword\">with<\/span> the html logger to testResults.html <span class=\"hljs-keyword\">in<\/span> the TestResults folder:\n$ dotnet test --logger <span class=\"hljs-string\">\"html;logfilename=testResults.html\"<\/span>\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the project <span class=\"hljs-keyword\">in<\/span> the current directory, and report tests that were <span class=\"hljs-keyword\">in<\/span> progress when the test host crashed:\n\n$ dotnet test --blame\n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the test1 project, providing the -bl (binary log) argument to msbuild:\n$ dotnet test ~<span class=\"hljs-regexp\">\/projects\/<\/span>test1\/test1.csproj -bl  \n\nRun the tests <span class=\"hljs-keyword\">in<\/span> the test1 project, setting the MSBuild DefineConstants property to DEV:\n$ dotnet test ~<span class=\"hljs-regexp\">\/projects\/<\/span>test1\/test1.csproj -p:DefineConstants=<span class=\"hljs-string\">\"DEV\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/github.com\/dotnet\/docs\/blob\/main\/docs\/core\/tools\/dotnet-test.md<\/li><li>https:\/\/learn.microsoft.com\/en-us\/dotnet\/core\/tools\/dotnet-test<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;dotnet test&nbsp;command is used to execute unit tests in a given solution. The&nbsp;dotnet test&nbsp;command builds the solution and runs a test host application for each test project in the solution. The test host executes tests in the given project using a test framework, for example: MSTest, NUnit, or xUnit, and reports the success or failure&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-31625","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=31625"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31625\/revisions"}],"predecessor-version":[{"id":31636,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31625\/revisions\/31636"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}