Search code examples
c#visual-studioresharperxunitvisual-studio-2022

Is there a way to quickly find out if a C# class has an xunit test on any of its methods in Visual Studio?


If I am looking at a class in Visual Studio, I want to know if any of its methods has a unit test on it. XUnit is being used.

Is there a way to quickly find out if a C# class has a unit test and on which method? Also, if there's a way to find out using Resharper, that would be helpful.


Solution

  • If you are using resharper this gets much easier. Make sure to build your code and then click on Extensions->Resharper->Unit Tests->Cover All Tests menu. This will build the coverage report.

    If everything runs without problems and your IDE is configured correctly, it starts reflecting test coverage characteristics -

    In your class you will see green lines next to your code to indicate that that part of code is covered -

    enter image description here

    On top of your method signature you should get the number of relevant tests that call the method -

    enter image description here

    You can click on the 'X/X passing' menu on top, it will list relevant tests in a popup -

    enter image description here

    From here you can navigate to individual tests as well as run/debug them.

    Also, you can right click inside the function code, in the context menu if you click on 'Show covering tests' menu, IDE populates list of tests associated with that function.

    Note that none of these features will work if your code is not building. The IDE and its extension requires metadata that is available only after your code builds successfully and tests are run.