How to run a single xunit C# test using dotnet test command line
Asked Answered
O

3

9

Background: I am trying to run a C# unit test (test is using Xunit Framework), and my requirement is to run a single unit test and not all the tests in the whole dll.

Things I tried: I have tried few commands, but with no luck till now.

  1. I tried dotnet test "complete_path/someTestDll.dll" Result : This starts running all the test in the dll (expected but not desired)

  2. Tried dotnet test "complete_path/someTestDll.dll" \Tests: "Namespace.ClassName.MethodToRun" Result: Dll containing the test found but no test matches the filter.

  3. Tried dotnet test "complete_path/someTestDll.dll" --filter "FullyQualifiedName=Namespace.ClassName.MethodToRun" No test matching filter is found (again path to dll is alright)

  4. Tried dotnet test "complete_path/someTestDll.dll" --filter "DisplayName=Namespace.ClassName.MethodToRun"

Not only these but various mix and match of these with complete path, relative path, etc. etc. and almost wasted whole day.

NOTE: I know there are few answers on this over SO, but I have tried them all, from last day, and nothing seems to be working for me till now, so I had to come here to get some help.

Looks like I am missing something serious, Hopefully, I can get some answers which solves my issue.

Thanks a lot!!

enter image description here

Ouzel answered 15/7, 2020 at 18:26 Comment(6)
Is it an option for you to just add [Fact(Skip = "Your reason to not run the test")] the skip parameter to your tests? I guess this wouldn't be ideal if you intend to run some of them later.Lallage
@JonathanVanDam Thanks for your answer, but this option is not feasible for me, as I have 4k unit tests in the dll in different classes and it is impractical to be adding this on each test. I just want a way to run a single unit test with providing its name for local testing purpose. Thanks!Ouzel
Hmm, the only other thing I can think of is using playlists inside of visual studio to create a set of unit tests that are to be run, but I'm unaware of a way to run the tests from the playlists from the command line.Lallage
okay, I am also not sure about that. Also, my whole intent to use command line is that I don't want to use Visual Studio as that is terribly slow. Thanks!Ouzel
I was able to get the FullyQualifiedName=Namespace.ClassName.MethodToRun to work. The only reason it didn't work was if I didn't have the fully qualified name put in correctly or the method wasn't a test. I wonder if there's something wrong with the fully qualified name.Lallage
Okay, here is something I have found out, that this command (as suggested by Stev here) is working for other tests, and not with just this test, though I am still not sure what is wrong with this. Need to check. and I guess the one you tried will work too. Thanks again!Ouzel
F
13

I was able to run a single xunit test via the developer command prompt using this template.

dotnet test "complete_path/someTestDll.dll" --filter "Namespace.ClassName.MethodName"

You can also run this command to see a full list of tests available, to help double check that the paths and names in your command are correct.

dotnet test "complete_path/someTestDll.dll" -t
Feathercut answered 15/7, 2020 at 18:53 Comment(2)
Thanks for answering but I tried this too and is not working. doing dotnet test "dllHere" -t lists all the tests, but again using the filter I get no tests found. Updated the question with image for the same.Ouzel
Okay, here is something I have found out, that this command is working for other tests, and not with just this test, though I am still not sure what is wrong with this. Need to check. I will update here with the findings. Thanks again!Ouzel
L
2
  1. Run inside the solution.

    dotnet test [project folder name] --filter=Namespace.ClassName.MethodName

  2. Run inside the project.

    cd [project folder name]

    dotnet test --filter=Namespace.ClassName.MethodName

Lectra answered 23/6, 2021 at 17:39 Comment(0)
S
0

with xUnit, you can simply run the following command to run scenarios with specific tags

dotnet test "(path to the test dll file)" --filter Category=scenarioTag

You can create conditions for the filters as long as you put them inside of a string.

dotnet test "(path to the test dll file)" --filter "Category=scenarioTag|Category=scenarioTag2"
Stegosaur answered 4/10, 2022 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.