Skip to content
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

[BUG] C# dev kit for xUnit not discovering tests #307

Closed
snowberry-clearwing opened this issue Aug 1, 2023 · 32 comments
Closed

[BUG] C# dev kit for xUnit not discovering tests #307

snowberry-clearwing opened this issue Aug 1, 2023 · 32 comments
Labels
area-test Test discovery, execution, debugging bug Something isn't working
Milestone

Comments

@snowberry-clearwing
Copy link

Describe the Issue

I am using the C# dev kit extension for unit testing with xUnit. It was able to discover the tests that were already in the project when the addon was installed, however I cannot discover new tests. I am not sure if I am missing a setting somewhere, however I could not find any relevant documentation apart from https://code.visualstudio.com/docs/csharp/testing.

Steps To Reproduce

  1. Created a new xUnit test project.
  2. Created a simple test.
  3. Built the test project to ensure it compiles
  4. Testing pane does not show new tests, even after hitting the 'Refresh Tests' icon.

Expected Behavior

When I hit the 'Refresh Tests' icon in the Test explorer I would see updated tests.

Environment Information

OS: WIndows 11
VS Code version: 1.80.2 (user setup)
Extension: v0.2.100

@snowberry-clearwing snowberry-clearwing added the bug Something isn't working label Aug 1, 2023
@microsoft-issue-labeler microsoft-issue-labeler bot added the area-test Test discovery, execution, debugging label Aug 1, 2023
@snowberry-clearwing
Copy link
Author

I figured why this was not working. The .sln file needed to be updated with the new test project. After I added this in Visual Studio, I could discover the tests in VS Code. Perhaps the aforementioned documentation could be checked to see if the setup instructions are correct? https://code.visualstudio.com/docs/csharp/testing.

@timheuer
Copy link
Member

timheuer commented Aug 1, 2023

Thanks for the additional info @snowberry-clearwing. Just to confirm you already had an existing solution open, but you created the new test project and it didn't automatically add it to your open solution?

@snowberry-clearwing
Copy link
Author

Yes, that is correct. I ran the 'dotnet add' command to ensure the project was added however the solution file still had to be fixed externally.

@timheuer
Copy link
Member

timheuer commented Aug 2, 2023

Thanks @snowberry-clearwing -- so your steps were:

  • Had an open solution
  • Created a new test project <-- what path did you take for this? Started from command pallette with new project? And not from the existing solution view right?
  • used dotnet add -- what command did you run here? dotnet sln add?

@ivanjx
Copy link

ivanjx commented Aug 3, 2023

dotnet clean immediately updates the tests pane for me.
for convenience, i do dotnet watch clean.

@AbhitejJohn AbhitejJohn added area-project Project load, Solution Explorer and removed area-test Test discovery, execution, debugging labels Aug 11, 2023
@kvenkatrajan kvenkatrajan added area-solution Solution explorer and removed area-project Project load, Solution Explorer labels Aug 17, 2023
@anuraj
Copy link

anuraj commented Oct 7, 2023

I am also facing the same issue. Just installed C# DevKit - I cloned this repo - https://github.com/anuraj/MinimalApi - In the editor I couldn't find any option to execute the tests.

image

I tried to remove the test project and added it again. I tried dotnet sln remove and dotnet sln add commands. But still VS Code is not showing any test options.

@timheuer
Copy link
Member

timheuer commented Oct 9, 2023

Hi @anuraj - in VS Code there is a "Test Explorer" area as seen here where the tests show up. After a build the tests will be discovered here:

image

@nwrkbiz
Copy link

nwrkbiz commented Oct 27, 2023

Hi @anuraj - in VS Code there is a "Test Explorer" area as seen here where the tests show up. After a build the tests will be discovered here:

image

I have the same issue, the tests do not show up within this view.

@Ulrika-nepa
Copy link

I can't get my new test to show up. If I run dotnet test, all the tests run, but the Test Explorer doesn't show it.
I've tried to reload the window, build the test project and run dotnet test.
(My sln file has a reference to the test project)

Only thing that works is restarting Vscode.

@wgrs
Copy link

wgrs commented Nov 22, 2023

This is still broken. None of my xunit tests are being discovered. Rebuilt. Re added to sln. Re installed vscode 1.84. Re installed dev kit. Still broken

@timheuer
Copy link
Member

This is still broken. None of my xunit tests are being discovered. Rebuilt. Re added to sln. Re installed vscode 1.84. Re installed dev kit. Still broken

@wgrs is this a repo perhaps we can look at? What target framework versions are the tests/projects?

@wgrs
Copy link

wgrs commented Nov 22, 2023

This is still broken. None of my xunit tests are being discovered. Rebuilt. Re added to sln. Re installed vscode 1.84. Re installed dev kit. Still broken

@wgrs is this a repo perhaps we can look at? What target framework versions are the tests/projects?

It's targeting 8. Calling dotnet test blows up with a missing testhost runtime json file error. Its a brand new install on Ubuntu 23.10. Should be easy to replicate.

@timheuer
Copy link
Member

This is still broken. None of my xunit tests are being discovered. Rebuilt. Re added to sln. Re installed vscode 1.84. Re installed dev kit. Still broken

@wgrs is this a repo perhaps we can look at? What target framework versions are the tests/projects?

It's targeting 8. Calling dotnet test blows up with a missing testhost runtime json file error. Its a brand new install on Ubuntu 23.10. Should be easy to replicate.

Ok, the blow up on the CLI alone could be a root cause here (stating the obvious). How did you install on Ubuntu? apt-get? Can you put the error on the dotnet-test issue here as well?

@wgrs
Copy link

wgrs commented Nov 22, 2023

Yes APT. Yes, will report on dotnet test

@wgrs
Copy link

wgrs commented Nov 23, 2023

This is the error being produced when I try and run dotnet test on Ubuntu on .Net 8. I have checked and the reference to the file below is missing.
Testhost process for source(s) '/home/xxx/Dev/Projects/xx/Tests/bin/Debug/net8.0/Tests.dll' exited with error: The specified runtimeconfig.json [/usr/lib/dotnet/sdk/8.0.100/testhost-8.0.runtimeconfig.json] does not exist . Please check the diagnostic logs for more information.

@timheuer
Copy link
Member

Hmm… @peterwald this seems more fundamental on the environment.

@timheuer timheuer added area-test Test discovery, execution, debugging and removed area-solution Solution explorer labels Nov 24, 2023
@AbhitejJohn AbhitejJohn added this to the Dec2023 milestone Nov 29, 2023
@gui-lisboa
Copy link

I faced the same problem having projects and .sln file under a src/ folder. Reverted C# Devkit to previous version and it's working again. (from 1.0.14 to 0.5.150)

@peterwald
Copy link
Member

This seems to be an issue with the local dotnet installation or configuration. I have not been able to reproduce the issues above. After running a build, the tests appear and are runnable in the test explorer pane. Closing due to lack of actionable details. Happy to re-investigate if more details come to light.

@earloc
Copy link

earloc commented Jan 17, 2024

@peterwald, Closing this as "not reproducable" probably won´t help those people facing the issue (like me today).

I had tests showing up in one solution, but not in another.

working solution:

  • assembly with TargetFramework: net6.0
  • test-assembly with TargetFramework: net8.0

non-working solution:

  • assembly with TargetFramework: net6.0
  • tests-assembly with TargetFramework: net7.0

Just got hands on a fresh dev-machine (MacOS Sonoma@M3 Max) and double checked everything against the former rig (Windows 11@Intel).

The former (windows-)rig (where both solutions were successfully discovering tests in test-explorer) has

  • net6.x
  • net7.x
  • net8.x

installed.

The new (MacOS)-machine only has

  • net6.x
  • net8.x

installed.

I then just modified the test-project to target net8.0and voila, tests are showing up again in the test-explorer.

Seems as if the matching target-framework of the test-assembly has to be installed on the machine in order to let test-explorer discover tests.
(I did not try out to install net7.x to see if this also would´ve solved the issue, though)

Hope this might help anybody else facing a similar situation.

@westsiderz
Copy link

westsiderz commented Jan 22, 2024

Facing the same issue here. I am using C# Dev Kit with Docker and Devcontainer. My machine is running Ubuntu but the project is opened inside the Docker container. I have net7.x The unit tests project is built with no issues. The Tests Explorer is not showing anything even if I refresh. As a matter of fact, it askes me to install additional plugin in order to be able to run tests.

So what I did was to open the folder, containing the Unit tests project in the Terminal and run dotnet test. After that the tests appeared in the Test Explorer. Now even if I close VSCode and open it again, it shows the tests

@earloc
Copy link

earloc commented Jan 22, 2024

@westsiderz Which version does your test-project built against? For me, the issue was solved once the machine had the proper sdk-version installed (or the test-project actually compiled against a version that is present on the machine, respectively).

Probably also the case for a devcontainer?!

So either switch your test-projects target-framework or add the matching. net-sdk in your Container might help?

@westsiderz
Copy link

@earloc

My Test-Project buids against Net7.x I am using the following in my Dockerfile

mcr.microsoft.com/devcontainers/dotnet:0-7.0

@cyates81
Copy link

I'm also having this issue. No matter what I try it says no test have been found even though the sln has the test project and dotnet test runs the tests properly. I am running in MacOS with all projects targeting 8.0.

@peterwald
Copy link
Member

The following steps should work to get some sample xUnit tests working as a baseline for comparison.

Prerequisites

  1. .NET 8 SDK is installed. Check version using dotnet --list-sdks. (this should work with most of the recent versions of the SDK as well, net 6 or net 7 for example).
  2. VS Code is installed and the C# Dev Kit extension is also installed in VS Code (tested with version v1.3.6)

Steps

  1. mkdir mytests
  2. cd mytests
  3. dotnet new xunit
  4. dotnet new sln
  5. dotnet sln add .
  6. dotnet test - Tests should run and succeed
  7. code . - Open VS Code in the test folder

After initialization is complete, the test explorer panel should show the sample test from this project.

@nestorbfgo
Copy link

nestorbfgo commented Feb 27, 2024

Thanks @snowberry-clearwing -- so your steps were:

  • Had an open solution
  • Created a new test project <-- what path did you take for this? Started from command pallette with new project? And not from the existing solution view right?
  • used dotnet add -- what command did you run here? dotnet sln add?

Maybe late but this work, I used this when I create a new test to my c# project, now it works correctly !

@omarmciver
Copy link

dotnet clean immediately updates the tests pane for me. for convenience, i do dotnet watch clean.

I find that I have to do an rm -rf '**/bin/** && rm -rf **/obj/** on occasions to get the C# DevKit test explorer to detect properly. The dotnet clean alone doesn't do it, which is weird. Might be Devcontainer related (building in it and out of it and causing file permission confusion).

@Sharaf0
Copy link

Sharaf0 commented Aug 8, 2024

Unfortunately I still have the same problem on mac, I tried every solution provided.

@Farazzaidi22
Copy link

There are a lot of mix response in this chat. What worked for me is a mix a couple of answers I saw.

  1. Run dotnet clean
  2. Run dotnet build

Then tests started to appear

image

@chrisdel101
Copy link

I also had this. Test explorer has been working and stopped all of a sudden. Build and clean did nothing. Restarting vscode did nothing. Even typing Tests in the command palette returned no results. There were no errors in the output panel. There was nothing left to try.

Two days later and a few restarts later the tests show up. So I cannot reproduce the issue either.

@nwrkbiz
Copy link

nwrkbiz commented Oct 31, 2024

For me the test project at least need to be net7.0 for the test runner to detect the unittests.

@amakhno
Copy link

amakhno commented Dec 25, 2024

Cleaning the test store folder helped. You can find it in the Output -> C# Dev Kit tab

Created Test Controller
Log verbosity: minimal
platform: win32 arch: x64
Using vstest from dotnet sdk in [C:\Program Files\dotnet\sdk\8.0.307].
Initialized Test Explorer Server [21964]
Test Store Folder: c:\Users\<Username>\AppData\Roaming\Code\User\workspaceStorage\8d49cae0d505b6684c6ba6a076c9a18c\ms-dotnettools.csdevkit

@WandersonAlves
Copy link

You're a live saver @amakhno !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-test Test discovery, execution, debugging bug Something isn't working
Projects
None yet
Development

No branches or pull requests