Live unit testing exclude tests - visual-studio-2017

I know it is possible to exclude the whole test project(s) from Live Unit Testing by right clicking on the test project and selecting the "Live Unit Testing" context menu.
But in my solution I have some long running/resource intensive tests, which I would like to exclude. Is it possible to exclude individual tests?

Easiest method is right clicking on the method in the editor view and selecting Live Unit Testing and Exclude.
You can also do it programatically with attributes.
For xUnit: [Trait("Category", "SkipWhenLiveUnitTesting")]
For NUnit: [Category("SkipWhenLiveUnitTesting")]
For MSTest: [TestCategory("SkipWhenLiveUnitTesting")]
more info at Microsoft docs

Adding onto adsamcik's answer, here's how you can exclude an entire project (like an integration tests project) programmatically:
via .NET Core's csproj file:
// xunit
<ItemGroup>
<AssemblyAttribute Include="Xunit.AssemblyTrait">
<_Parameter1>Category</_Parameter1>
<_Parameter2>SkipWhenLiveUnitTesting</_Parameter2>
</AssemblyAttribute>
</ItemGroup>
// nunit
<ItemGroup>
<AssemblyAttribute Include="Nunit.Category">
<_Parameter1>SkipWhenLiveUnitTesting</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
// mstest
<ItemGroup>
<AssemblyAttribute Include="MSTest.TestCategory">
<_Parameter1>SkipWhenLiveUnitTesting</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
or via assemblyinfo.cs:
// xunit
[assembly: AssemblyTrait("Category", "SkipWhenLiveUnitTesting")]
// nunit
[assembly: Category("SkipWhenLiveUnitTesting")]
// mstest
[assembly: TestCategory("SkipWhenLiveUnitTesting")]
I figured out how to add the assembly attribute in .net core's csproj file via this SO answer. The attributes came from MS's documentation.

Related

DotNet Core run test once at time

I'm looking for a way for my xUnits to run tests one at a time (dotnet test (version core 6)). Both from code and configuration.
In particular, the tests will have to be launched via an action on github
Thanks
You could use xunit.runner.json file:
{
"parallelizeAssembly": false,
"parallelizeTestCollections": false
}
And then in your csproj
<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
You can also write in code
[assembly: CollectionBehavior(DisableTestParallelization = true)]

How do I get a console project to group my appsettings.json files?

If I start a new web api project, the appsettings files are grouped together. However, I'm creating a working project from the console app template and when I create the appsettings files manually, the do not group together. I think back in older versions, there was something I'd put in the csproj file, but I don't know how to do it in .net core and I'm not seeing anything in properties or configurations
In the project file of your solution you can edit or add an <ItemGroup> element within the <Project> element. This worked for me:
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="appsettings.*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DependentUpon>appsettings.json</DependentUpon>
</Content>
</ItemGroup>
Please Note that my console project targets .Net Core 2.0 and is running in Visual Studio Pro 2017 Version 15.7.5.
Also, if your Solution Explorer doesn't immediately refresh try unloading and reloading the project.
Using an <ItemGroup> with <Content> as suggested gave me an error (in Visual Studio 2019) about "Duplicate 'Content' items included". It turns out the .NET SDK includes 'Content' items from your project directory by default. Setting the EnableDefaultContentItems property to false seems a bit rigid, so now I include the items as <None>.
<ItemGroup>
<!-- Group AppSettings in Console project. Use None to prevent "Duplicate 'Content' items were included" when using (default) EnableDefaultContentItems=true -->
<None Include="appsettings.*.json">
<DependentUpon>appsettings.json</DependentUpon>
</None>
</ItemGroup>
This does show the files grouped, but shows their properties with Build Action 'None' and 'Do Not Copy' in the Solution-explorer, so I guess that's the price for wanting them to group?
FWIW: a file-nesting rule as suggested in appsettings-json-not-in-hierarchy
will not show the files as grouped/nested, but it will make it collapse if the solution-explorer collapse-button is pressed.
You just need to click the File Nesting icon, and choose "Web"

Custom ProjectSchemaDefinitions in Project Property Sheet

I finally managed to configure my Visual Studo C++ Project to show my Custom ProjectSchemaDefinition if I take a look at a Single Project->Property.
My Question:
Is there any way to show this in a Shared Project Property (eg external.props) ?
My current shared.targets configuration
<PropertyGroup Label="Import Settings">
<UseDefaultProjectTools>true</UseDefaultProjectTools>
<UseDefaultPropertyPageSchemas>true</UseDefaultPropertyPageSchemas>
<UseDefaultGeneralPropertyPageSchema>true</UseDefaultGeneralPropertyPageSchema>
</PropertyGroup>
<ItemGroup>
<PropertyPageSchema Include="$(SolutionDir)\props\PublishConfig.xml">
<Context>Project</Context>
</PropertyPageSchema>
<ProjectTools Include="PublishConfig" />
</ItemGroup>
I think I miss just the right Context in this definition but cannot find any reliable source at msdn where different Context Targets are correctly described.
Thank you in advance

Running unit tests for typescript in VS

I've found couple of examples of running unit tests for typescript.
All of them are based on referencing both ts and js file like
/// <reference path="../Calc.ts" />
/// <reference path="../Calc.js" />
Unfortunately when I try to reference js file I am getting the following error:
Incorrect reference: referenced file: "../Calc.js" cannot be resolved.
If there is no reference test runner doesn't load js file under test and test fails to execute.
Any ideas? Ideally I want to run tests in VS test explorer or Resharper test session.
You can use Chutzpah for that
With Chutzpah you can run your tests from command line and integrate your tests with Visual Studio Test Explorer.
Chutzpah allows you decide if you want to run the tests from .ts files, .js files, .html files or from all of them.
It also allows you to load external .js files (e.g. dependent libraries) from your .ts unit test file, with its specific :
/// <chutzpah_reference path="lib/jquery-1.9.1.min.js" />
/// <reference path="src/YourFileToBeTested.ts" />
Your unit tests can be written in TypeScript.
You can install Chutzpah from Visual Studio/Tools/Extensions and updates.
There is absolutely no reason to include a reference to a JS file :
/// <reference path="../Calc.js" />
The use for /// <reference is to provide the compiler with information about type info present in another file. It has no runtime implications, only compile time implications. And all the compile time implications (typeinfo, code generation) are taken into account when you did:
/// <reference path="../Calc.ts" />

SpecFlow unit test failed due to not able to find "TechTalk.SpecFlow" file

I have a VS2010 unit test project set to using SpecFlow 1.8.1 and mstest. In order to get the SpecFlow unit tests working, I've done the following:-
I added the references to the following files in my project:-
Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
TechTalk.SpecFlow.dll
Note that the TechTalk.SpecFlow.dll has been added into my project and the reference points to that file.
I've set the "Copy Local" property of the TechTalk.SpecFlow.dll reference to True.
I've also added an App.Config that specifies "MsTest.2010" as the provider, and regenerated all code-behinds for the SpecFlow features.
Everything works in my VS2010, the tests run successfully in both the SpecFlow testrunner and the mstest test runner. BUT when I try to run the mstests in TFS 2008 (using a .vsmdi test list file), it failed with the following exception:-
Class Initialization method MyNamespace.MyTestFeature.FeatureSetup threw exception.
System.Configuration.ConfigurationErrorsException:
System.Configuration.ConfigurationErrorsException: An error occurred creating the
configuration section handler for specFlow: Could not load file or assembly
'TechTalk.SpecFlow' or one of its dependencies. The system cannot find the file
specified. (D:\Projects\TestProject\TestResults\administrator_MYPC 2012-06-27
18_30_05_Any CPU_Debug\Out\TestProject.DLL.config line 4) --->
System.IO.FileNotFoundException: Could not load file or assembly 'TechTalk.SpecFlow'
or one of its dependencies. The system cannot find the file specified.
Note that the TFS built the project fine and it runs other unit tests in the same project (normal mstests, not SpecFlow) without problems. It only failed for the SpecFlow test runs.
So what am I doing wrong?
Edit: The contents of my App.Config file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="specFlow"
type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow"/>
</configSections>
<specFlow>
<unitTestProvider name="MsTest.2010" />
<runtime detectAmbiguousMatches="true"
stopAtFirstError="false"
missingOrPendingStepsOutcome="Inconclusive" />
<trace traceSuccessfulSteps="true"
traceTimings="false"
minTracedDuration="0:0:0.1" />
</specFlow>
</configuration>
Following the instruction on this site and this site:
the command Tools > Library Package Manager > Package Manager Console allows you to type in PM> Install-Package SpecFlow
when the prompts returns "installed successfully", the SpecFlow Assembly now appears in the references of your project. And the MSTest project now compiles succesfully (at least for me).
I got this error as well, in my case the problem was that I was using the \...\obj\Debug||Release\ folder as target and not the \...\bin\Debug||Release\ folder. Looking in these folders I saw that the TechTalk.dll assembly was missing from the former. Simply switching in my .bat file the problem was fixed.
Sometimes VS2013 is looking for SpecRun dlls not in project folder, but in C:\Users\**YOUR_USER**\AppData\Local\Temp\VisualStudioTestExplorerExtensions\SpecRun.Runner.1.3.0\tools. So you just need to put all necessary SpecFlow libraries therel
One hack I found to get it working is to add another class for EVERY single SpecFlow feature that I created in the project. The class looks like this:-
[DeploymentItem(#"TechTalk.SpecFlow.dll")]
partial class MyTestFeature { }
// The above class-name needs to come from the auto-generated code behind
// (.feature.cs) for each SpecFlow feature.
I consider this as a very nasty hack, but it does provide a clue as to why it didn't work. It would be good if anyone comes up with a more elegant solution.
I finally found the more proper fix for this issue. I just need to add a post-build event to remove the .config file from the build output. (The App.config file is used only to generate the code-behind during design time. It is not used at all during runtime, so it can be removed.)
The command for the post-build event looks like this:-
del /f /q "$(TargetDir)$(TargetFileName).config"
Correction: The .config file is used for generating inconclusive results, so a better post-build event command is as follows:-
if "$(IsDesktopBuild)"=="false" del /f /q "$(TargetDir)$(TargetFileName).config"