Don't display class name in Visual Studio Test Explorer - visual-studio-2017

I am using Xunit in my vs project. In my Test explorer, I see this:
Fully.Qualified.Namespace.ClassName.TestName1
Fully.Qualified.Namespace.ClassName.TestName2
etc.
I would much rather just see this:
TestName1
TestName2
etc.
or even this (2nd best, but still a big improvement):
ClassName.TestName1
ClassName.TestName2
etc.
Is that possible? If so, how?

add a file to the root directory of your test project named xunit.runner.json
with the following content:
{
"methodDisplay": "method"
}

Related

Setup/Configuring unit-testing with google test in C++ using Visual Studio 2020

If you can't get your solution to compile, for example by receiving an unresolved externals error, take a look at the answer section and recreate the steps listed there.
Our example header:
#pragma once
#include <string>
std::string testfunc();
Our example sourcefile:
#include "to_test.h"
std::string testfunc()
{
return "test worked";
}
After creating our example project, we wanna check some things on our list beforehand.
add google test adapter via visual studio installer (under individual components, search for "google")
right click our test project inside of our solution and click on "manage NuGet Packages", switch to the Browse tab and search for "gtest" and add it. It looks sorta like this:
we then want to add a unit test project to our solution. we right-click our solution in the solution explorer and choose add->new project. We search for "google" and add the one things that pops up which is named "Google Test". For a start we keep every setting on default, except for the path which we are going to change from the parent-folder of the solution to the folder of the project we are going to test (bacially just one depth deeper). We will open our test.cpp and add it somewhat like this: (note: the #include of your custom header shouldn't be copy-pasted to make sure its the right path in your case)
#include "pch.h"
#include "../to_test.h"
TEST(test, TestName)
{
//This Test will work
EXPECT_TRUE(testfunc() == "test worked");
//This Test will fail
EXPECT_TRUE(testfunc() == "test not worked");
}
now for the configuration: right-click on your test-project and open the properties.
under VC++ Directories, add the location of your header files under the "Include Directories"
under Linker->General, add the Debug-Folder of your Project which is to be tested under the "Addtional Library Directories"
under Linker->Input simple add the name of your headerfile without the filetype to the "Additional Dependencies" in our case that would be "to_test" (without the quotes)
We can then right-click our solution and rebuild it. After that we can choose our GTest-1 Project as the Startproject via right-clicking it and then debug it as usual. The Terminal popping up should look sorta like this:
DISCLAIMER: This definietly isn't the only way to do this.. if someone cares to correct me i would highly appreciate it :)

Visual Studio Code's "Run Test | Debug Test" option missing from unit tests

I used to see "Run Test | Debug Test" links top of every test function in VS Code for Go. But they are missing now. How can I re-enable them?
This picture shows what I am talking about:
Check your options to confirm that the Go test explorer is enabled:
Preferences > Settings > User Settings > find "go test enable code lens"
Taken directly from the go.dev doc for the testing package
To write a new test suite, create a file whose name ends _test.go that contains the TestXxx functions as described here. Put the file in the same package as the one being tested.
Please ensure that your test file name ends in _test.go, and that the function you're trying to test starts with Test (like it does in your snippet).
Had the same issue today. In my case opening the Command Palette (Ctrl+Shift+P) and running "OmniSharp: Restart OmniSharp" fixed it.
Same thing here - simply closed down VS Code and opened it back up again... they all appeared once more.
This happened to me, though I was writing Javascript (not Go).
To fix that, I need to make sure the Es6 import & export statements are correct (-- correctly importing the correct item from the correct file path),, -- for this current test js file & relevant imported js files (or even all files in current project).
(Then you must save the test js file to refresh it.)
The Test Explorer's Output panel may(?<) throw an Error to indicate that.
I have this situation when there are many folders in the project I opened in VScode. But these options appear only when I open the folder containing the script I want to run the test in in a new window in VSCode. I don't know why, it's weird.

How to disable auto save on test file for visual studio code extension test

I have been working on a extension for Visual Studio Code (v1.15.0). It contains commands (functions) to change HTML code in the VSCode editor window, for code conversion purposes.
I decided to extend my unit test code with an test-data.html file. So the test are running on a prepared HTML document, to test the commands and results.
My launch.json file, contains something like
... "args": ["${workspaceRoot}/test/test-data.html", "--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ], ...
Everything works fine, except for the fact that after the test (with tested code changes), the test file is saved. Ruin the original test data set.
I would like to disable the auto save in the 'Extension Development Host' environment, when my test are run. Can we do that or maybe, introduce a couple of undoes?
Or create a copy in the ./out folder and use that? Then how to do that.
Aug. 18, additional note
It's a strange problem. I'm working on this project at home and at work. Different machines. VSC is same version, maybe different extensions.
This problem of compromising my original test data, occurs only when working at Home. On the work PC everything works as expected, test data stays as it is.
Maybe something like 'auto save', I need to investigate.
Sep. 8, additional note
Nailed it, it was the 'Auto Save' option turned on. Not sure why it was set. Problem solved (I also implemented a copy function, which solved the problem also).
Use fs-extra to copy your test-data.html file to the out folder and then use the same command you used to load it, just change /test to /out.

How to show only method name in Test Explorer in VS 2017 (using xUnit for .NET Core)

I know there is this question:
How can XUnit be configured to show just the method name in the Visual Studio 2015 Test Explorer?
I tried both the solution using XML and the JSON file but the name in Text Explorer Window is still the full name with the class. I want to display the method name only as its hard to read the fully qualified names.
Its stated on this site that you can configure using XML
Configuring xUnit.net with XML
but I can't make the effect I'm expecting happen. I've restarted VS 2017 after adding an app.config file in the test project, but still nothing. Is it different for VS 2017?
I had the same issue. I'm doing a project in VS2017 using .NET Standard and solved it by following these steps:
In your tests project, create a file named xunit.runner.json
Add the following to the file: { "methodDisplay" : "method" }
In the Solution Explorer, right-click on xunit.runner.json and select "Properties". Set Copy to Output Directory to "Copy Always".
Taken from this comment.
W. Hampson answer is perfect, but just to inform about other possibility - use DisplayName attribute.
[Fact(DisplayName = "Just simple check")]
public void Check()
{
Assert.NotNull(_operation);
}

Roslyn workspace.OpenSolutionAsync().Projects always empty?

I'm trying to create a self-hosted WebAPI 2.0 project that allows you to open/explore/build .sln solutions through an API.
Here's the code within one of my controllers, that's supposed to return a list of projects given a path to the .sln:
public async Task<IHttpActionResult> GetProjects(string slnPath = "")
{
var workspace = MSBuildWorkspace.Create();
var solution = await workspace.OpenSolutionAsync(slnPath);
var projects = solution.Projects;
}
I would expect projects to hold the projects in the solution, but according to the debugger, solution.Projects and solution.ProjectIds always seem to be empty.
I've tried this with multiple .sln files, all of which I can open in Visual Studio and see that they have projects in them.
I've seen this question, but my project isn't a Visual Studio add in, it's a class library called from a command line application.
This is generally caused by one of a few things, in order of commonality:
You are missing copies of Microsoft.CodeAnalysis.CSharp.Workspaces.dll or Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll. Make sure when you are running your project that those DLLs are next to the main Microsoft.CodeAnalysis.Workspaces.dll.
You're loading solutions with project types we don't support. We should support any project type except the project-less Web Site projects. Class libraries should work just fine.
We have a bug that's causing us to mis-handle your particular project types. If that's the case, file a bug on GitHub.
If you are getting this issue in unit tests but not in your production code, ensure that you have referenced both Microsoft.CodeAnalysis.CSharp.dll and Microsoft.CodeAnalysis.CSharp.Workspaces in your test project.
JYL mentioned it in the comments of the selected answer but I didnt see it right away.