Unable to run specflow tests using vstest.console.exe - visual-studio-2017

I am trying to run speflow tests based on visual studio runner but getting error in vsts. I am using the following vsts test runner task:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe myassembly.dll
Error:
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types
Retrieve the LoaderExceptions property for more information.
I have tried:
/TestAdapterPath: "Adapterpath"/UseVsixExtensions:True/False(Both option)/Platform:[ platform type ]
... but none of these are working; tests are working fine in Visual Studio 2017, but are still not working in the CI job.

Below lines of code helped me in identifying the missing dll now it's build which need to be fixed.
try
{
}
catch (System.Reflection.ReflectionTypeLoadException ex)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
System.IO.FileNotFoundException exFileNotFound = exSub as System.IO.FileNotFoundException;
if (exFileNotFound != null)
{
if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
throw new System.Exception(errorMessage);
}

Related

Visual Studio for Mac not running unit tests from MSTest project

I've created a .NET Core 6 Web API project in Visual Studio for Mac. I can run the web API without issues and can interact with it using Swagger. I'm now attempting to create a MSTest project and write unit tests. I've written one however when I attempt to run it, Visual Studio for Mac appears not to do anything like so:
Unit Test:
namespace Tests;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void ExampleTest()
{
var text = "hello.";
var result = text.ToUpperInvariant();
Assert.AreEqual("HELLO.", result);
}
}
Any ideas on what I'm missing?

Facing Test Case not found on Visual Studio 2017 [version 15.8.1]

I am facing "test method not found" on visual studio 2017 - Version 15.8.1, When i am trying to run or debug the code from visual studio.
Issue: [Test project {Project Name} does not reference any .NET NuGet adapter. Test discovery or execution might not work for this project. It is recommended to reference NuGet test adapters in each test project in the solution. ]
Note: I am able to solve the above error on visual studio 2017 versions 15.7.* using below link.
Please note this is not duplicate of - "Visual Studio 17: Facing Test Case not found issue in Visual Studio 17" or any other question already in stack overflow.
If you have the same version. It can be reproduced using this code.
[TestClass]
public class ATest : ATestBase
{
[TestInitialize]
public override void TestInitialize()
{
if (Playback.IsInitialized == false)
Playback.Initialize();
}
[TestCleanup]
public override void TestCleanup()
{
if (Playback.IsInitialized == true)
Playback.Cleanup();
base.TestCleanup();
}
[TestMethod, TestCategory("SmokeTest")]
[DeploymentItem(#"DataSource\Documents\some.pdf")]
public void SmokeFlow()
{
string name = "test";
}
}
Starting Visual studio 2017 version 15.8 and on wards. A new option is added under tools -> options -> test as shown in screenshot. Uncheck the option and restart your visual studio. This solves the issue.

ConfigurationManager issue in VS 2017

The following code, when run in Visual Studio 2015 and earlier, results in a message box being shown with the expected value "12345".
string executablePath = Application.ExecutablePath;
executablePath = Path.GetFileNameWithoutExtension(executablePath);
executablePath = executablePath + ".vshost.exe";
if (!File.Exists(executablePath))
throw new FileNotFoundException(executablePath);
Configuration cfg = ConfigurationManager.OpenExeConfiguration(executablePath);
cfg.AppSettings.Settings.Add("Testing", "12345");
cfg.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(cfg.AppSettings.SectionInformation.Name);
string testing = ConfigurationManager.AppSettings["Testing"];
MessageBox.Show(testing);
When I run the same code in Visual Studio 2017 the message box displays a blank value.
Is this a bug in Visual Studio 2017 or does the code require modification?
UPDATE (specific cause):
So the main cause, along with the accepted answer, was that I had opened the solution in VS 2015 which generated the *.vshost.exe related files. Later I opened the solution in VS 2017 and, of course, the *.vshost.exe files aren't cleaned automatically so were still there.
UPDATE 2 (for those who want to be able to use similar code in both):
string executablePath = Application.ExecutablePath;
executablePath = Path.GetFileNameWithoutExtension(executablePath);
executablePath = executablePath + ".vshost.exe";
// Check if the *.vshost.exe exists
if (File.Exists(executablePath))
{
try
{
// If deleting throws an exception then the stub is being run by *.vshost.exe while
// debugging which means this is NOT Visual Studio 2017 (*.vshost.exe is no longer used in VS 2017)
File.Delete(executablePath);
// If it deletes then use the regular app path since VS2017 is using that now.
executablePath = Application.ExecutablePath;
}
catch (Exception)
{
executablePath = Application.ExecutablePath;
}
}
else
executablePath = Application.ExecutablePath;
Configuration cfg = ConfigurationManager.OpenExeConfiguration(executablePath);
cfg.AppSettings.Settings.Add("Testing", "12345");
cfg.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(cfg.AppSettings.SectionInformation.Name);
string testing = ConfigurationManager.AppSettings["Testing"];
MessageBox.Show(testing);
The debugger hosting process has been removed in VS2017, so when you run your application the path you give to the OpenExeConfiguration method is incorrect.
Instead, pass Application.ExecutablePath to that method and it should work. The same should work in VS 2015 if you turn off the hosting process (Project properties -> Debug -> Enable the Visual Studio hosting process).
It's odd that you were using the hosting process path in the first place, as that would only ever work while running in the debugger from within Visual Studio.

Experiencing "DEP3000" error when trying to run WinRT unit tests

I am experience this strange error trying to run unit tests for WinRT from Visual Studio 2012.
Error: DEP3000: Attempts to stop the application failed. This may
cause the deployment to fail. App Packages may only be shutdown as
part of a Visual Studio build operation
I don't understand at all what that could mean? I've tried restarting my computer and reinstalling developer license. How I reproduce it is I just create a new empty unit test project from the Visual Studio template. I don't touch the project at all. But, when I try to do Run Tests on it, it just says pending for a a while and then spits out that error
How do I fix this?
Specs: Windows 8 Enterprise 64bit, Visual Studio 2012 with Update 2
Also, I've seen this question about VS2012RC, but the answer doesn't seem to apply. I've tried every configuration of this there is and nothing works past this error
I had same problem when I applied VS upgrade2 AND used Resharper 7.1.1 to run the unittests. Turns out you need to upgrade Resharper to 7.1.3 -- or run the tests with the VS test explorer.
You can just try using XUnits App:
https://channel9.msdn.com/Shows/demooftheday/xunit-in-uwp
With this, test will run on XUnit's test app, not in VS Test Explorer.
In my unit test project's project.json
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0",
"xunit": "2.1.0",
"xunit.runner.devices": "2.1.0" }, "frameworks": {
"uap10.0": { } }, "runtimes": {
"win10-arm": { },
"win10-arm-aot": { },
"win10-x86": { },
"win10-x86-aot": { },
"win10-x64": { },
"win10-x64-aot": { } }
}
Then go to the XAML file in the unit test project named UnitTestapp.xaml and change it to
<ui:RunnerApplication
x:Class="UnitTestProject1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UnitTestProject1"
xmlns:ui="using:Xunit.Runners.UI"
RequestedTheme="Light">
</ui:RunnerApplication>
And of course, in the code behind:
sealed partial class App : RunnerApplication
{
protected override void OnInitializeRunner()
{
AddTestAssembly(GetType().GetTypeInfo().Assembly);
InitializeRunner();
}
partial void InitializeRunner();
}

Unhandled MethodAccessException using Moles and xUnit.net to run unit tests

I have a unit test project in Visual Studio 2010 (.NET 4) that utilizes the xUnit.net testing framework, Moq, and the Moles Isolation framework for generating stubs of static methods. I am using xUnit version 1.9 on a 64-bit machine.
To run tests from the command line, I am using the following command:
moles.runner.exe Project.Tests.dll /runner:xunit.console.clr4.exe
However, I get the following exception every time:
instrumenting...started xUnit.net console test runner (64-bit .NET
4.0.30319.1) Copyright (C) 2007-11 Microsoft Corporation.
Unhandled Exception: System.MethodAccessException: Attempt by security
transparent method 'Xunit.ConsoleClient.Program.Main(System.String[])'
to access security critical method
'System.AppDomain.add_UnhandledException(System.UnhandledExceptionEventHandler)'
failed. at Xunit.ConsoleClient.Program.Main(String[] args) at
Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner,
String[] args) at
Microsoft.Moles.Runner.MolesRunner.RunnerRunner.Run(String runner,
String[] args) at
Microsoft.Moles.Runner.MolesRunner.LaunchRunnerEntryPoint(MolesRunnerOptions
options) at Microsoft.Moles.Runner.MolesRunner.RunnerMain(String[]
args) at Microsoft.Moles.Runner.Program.Main(String[] args)
It looks like the exception is coming from xUnit; however, I am able to run the tests using xunit.console.clr4.exe alone without issue. It only fails when using the xUnit console from the Moles runner.
I found this on a forum post:
In the .NET 4 framework, security tranparency rules prevent any
security transparent code from calling into security critical code.
What can I check to determine the cause of this error? Is there a security setting I need to change to prevent this?
Note: I am also having the same exact problem on a 32-bit workstation.
Update: I decided to download the code from http://xunit.codeplex.com/SourceControl/changeset/changes/600246119dca and debug myself. In the xunit.console project (the output of which is the exe getting invoked from the Moles runner), the main thread of execution looks like this:
[STAThread]
public static int Main(string[] args)
{
Console.WriteLine("xUnit.net console test runner ({0}-bit .NET {1})",
IntPtr.Size * 8, Environment.Version);
Console.WriteLine("Copyright (C) 2007-11 Microsoft Corporation.");
if (args.Length == 0 || args[0] == "/?")
{
PrintUsage();
return -1;
}
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
try
{
CommandLine commandLine = CommandLine.Parse(args);
int failCount = RunProject(commandLine.Project,
commandLine.TeamCity, commandLine.Silent);
if (commandLine.Wait)
{
Console.WriteLine();
Console.Write("Press any key to continue...");
Console.ReadKey();
Console.WriteLine();
}
return failCount;
}
catch (ArgumentException ex)
{
Console.WriteLine();
Console.WriteLine("error: {0}", ex.Message);
return -1;
}
catch (BadImageFormatException ex)
{
Console.WriteLine();
Console.WriteLine("{0}", ex.Message);
return -1;
}
}
When I run my tests while debugging the code, everything works fine, as expected (since I never had issues running tests from xUnit alone). I noticed the following line, which appears to be where the exception is thrown based on the error message and stack trace from my original post:
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
I commented out this line, built xunit.console.exe, and tried using it as the /runner argument when executing the Moles runner again. This time, no exception was thrown.
I am still at a loss as to why a Security exception is being thrown on this line when invoked from moles.runner.exe, but not when I run the xUnit console by itself.
Compiling from the latest source code from CodePlex runs without the MethodAccessException.