Пример #1
0
        /// <summary>
        /// Returns the VsTestConsole Wrapper.
        /// </summary>
        /// <returns></returns>
        public IVsTestConsoleWrapper GetVsTestConsoleWrapper()
        {
            var logFileName = Path.GetFileName(Path.GetTempFileName());
            var logFileDir  = Path.Combine(Path.GetTempPath(), "VSTestConsoleWrapperLogs");

            if (Directory.Exists(logFileDir) == false)
            {
                Directory.CreateDirectory(logFileDir);
            }

            var logFilePath = Path.Combine(logFileDir, logFileName);

            Console.WriteLine($"Logging diagnostics in {logFilePath}");

            string consoleRunnerPath;

            if (this.IsNetCoreRunner())
            {
                consoleRunnerPath = Path.Combine(this.testEnvironment.PublishDirectory, "vstest.console.dll");
            }
            else
            {
                consoleRunnerPath = this.GetConsoleRunnerPath();
            }

            var vstestConsoleWrapper = new VsTestConsoleWrapper(consoleRunnerPath, Path.Combine(this.testEnvironment.ToolsDirectory, @"dotnet\dotnet.exe"), new ConsoleParameters()
            {
                LogFilePath = logFilePath
            });

            vstestConsoleWrapper.StartSession();

            return(vstestConsoleWrapper);
        }
Пример #2
0
        //[Test]
        public async Task Sandbox()
        {
            var fileSystem = new FileSystem();

            var vstest = new VsTestConsoleWrapper(@"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe");

            vstest.InitializeExtensions(Enumerable.Repeat(@"C:\Users\Jake\Documents\Visual Studio 2017\Projects\nunit3-vs-adapter\src\NUnitTestAdapter\bin\Debug\net35\NUnit3.TestAdapter.dll", 1));

            var testCaseDiscoveryManager = new TestCaseDiscoveryManager(vstest);
            var testCaseManager          = new TestResultManager(fileSystem, testCaseDiscoveryManager);

            var testCasesDiscoveredHandler = Substitute.For <EventHandler <DiscoveredTestsEventArgs> >();

            testCaseDiscoveryManager.TestCasesDiscovered += testCasesDiscoveredHandler;
            var discoveryCompleteHandler = Substitute.For <EventHandler <DiscoveryCompletedEventArgs> >();

            testCaseDiscoveryManager.DiscoveryCompleted += discoveryCompleteHandler;
            var messageReceivedHandler = Substitute.For <EventHandler <TestRunMessageEventArgs> >();

            testCaseDiscoveryManager.MessageReceived += messageReceivedHandler;

            var testCasesAddedHandler = Substitute.For <EventHandler <TestResultsEventArgs> >();

            testCaseManager.TestResultsAdded += testCasesAddedHandler;
            var testCasesModifiedHandler = Substitute.For <EventHandler <TestResultsEventArgs> >();

            testCaseManager.TestResultsModified += testCasesModifiedHandler;
            var testCasesRemovedHandler = Substitute.For <EventHandler <TestResultsEventArgs> >();

            testCaseManager.TestResultsRemoved += testCasesRemovedHandler;

            await testCaseManager.AddSourceAssemblyPathAsync(@"C:\Users\Jake\Documents\Visual Studio 2017\Projects\FaultDictionaryDebugger\CommonUtilitiesTests\bin\Debug\CommonUtilitiesTests.dll");

            CollectionAssert.IsNotEmpty(testCaseManager.TestResults);
        }
Пример #3
0
        /// <summary>
        /// Returns the VsTestConsole Wrapper.
        /// </summary>
        /// <returns></returns>
        public IVsTestConsoleWrapper GetVsTestConsoleWrapper()
        {
            var vstestConsoleWrapper = new VsTestConsoleWrapper(this.GetConsoleRunnerPath());

            vstestConsoleWrapper.StartSession();

            return(vstestConsoleWrapper);
        }
Пример #4
0
        public static void Main(string[] args)
        {
            // Spawn of vstest.console with a run tests from the current execting folder.
            var executingLocation = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            var runnerLocation    = Path.Combine(executingLocation, "vstest.console.exe");
            var testadapterPath   = Path.Combine(executingLocation, "Adapter\\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll");
            var testAssembly      = Path.Combine(executingLocation, "UnitTestProject.dll");

            IVsTestConsoleWrapper consoleWrapper = new VsTestConsoleWrapper(runnerLocation);

            consoleWrapper.StartSession();
            consoleWrapper.InitializeExtensions(new List <string>()
            {
                testadapterPath
            });

            var testCases = DiscoverTests(new List <string>()
            {
                testAssembly
            }, consoleWrapper);

            Console.WriteLine("Discovered Tests Count: " + testCases?.Count());
            Console.WriteLine("Discovered Test: " + testCases?.FirstOrDefault()?.DisplayName);

            Console.WriteLine("-------------------------------------------------------");

            var testresults = RunSelectedTests(consoleWrapper, testCases);

            Console.WriteLine("Run Selected Tests Count: " + testresults?.Count());
            Console.WriteLine("Run Selected Test Result: " + testresults?.FirstOrDefault()?.TestCase?.DisplayName + " :" + testresults?.FirstOrDefault()?.Outcome);

            Console.WriteLine("-------------------------------------------------------");

            testresults = RunAllTests(consoleWrapper, new List <string>()
            {
                testAssembly
            });

            Console.WriteLine("Run All Test Count: " + testresults?.Count());
            Console.WriteLine("Run All Test Result: " + testresults?.FirstOrDefault()?.TestCase?.DisplayName + " :" + testresults?.FirstOrDefault()?.Outcome);

            Console.WriteLine("-------------------------------------------------------");

            testresults = RunTestsWithCustomTestHostLauncher(consoleWrapper, new List <string>()
            {
                testAssembly
            });

            Console.WriteLine("Run All (custom launcher) Test Count: " + testresults?.Count());
            Console.WriteLine("Run All (custom launcher) Test Result: " + testresults?.FirstOrDefault()?.TestCase?.DisplayName + " :" + testresults?.FirstOrDefault()?.Outcome);

            Console.WriteLine("-------------------------------------------------------");
        }
Пример #5
0
        static async Task <int> MainAsync(string[] args)
        {
            if (args.Length != 1)
            {
                Console.Error.WriteLine("Usage: TestRunner.exe <path to assembly to be tested>");
                return(-1);
            }

            var pathToAssembly         = Path.GetFullPath(args[0]);
            var targetFrameworkVersion = GetTargetFrameworkVersion(pathToAssembly);

            Console.WriteLine("Will test assembly that targets {0}", targetFrameworkVersion);

            var pathToCurrentAssembly = Path.GetDirectoryName(new Uri(typeof(Program).Assembly.CodeBase).LocalPath);
            var pathToVSTest          = Path.GetFullPath(Path.Combine(pathToCurrentAssembly, "..", "TestPlatform", "Microsoft.TestPlatform.15.0.0", "tools", "net46", "vstest.console.exe"));

            if (!File.Exists(pathToVSTest))
            {
                Console.Error.WriteLine("Could not find vstest.console.exe. Make sure you have run build.cmd to restore it from NuGet.");
                return(-2);
            }

            var logFilePath = Path.Combine(pathToCurrentAssembly, "..", FormattableString.Invariant($"VSTest_{Path.GetFileName(pathToAssembly)}_{DateTime.Now:yyyyMMdd-HHmmss.fff}.log"));

            var console = new VsTestConsoleWrapper(pathToVSTest, new ConsoleParameters {
                LogFilePath = logFilePath
            });

            console.StartSession();

            var pathToAssemblyDirectory = Path.GetDirectoryName(pathToAssembly);
            var extensions = Directory.GetFiles(pathToAssemblyDirectory, "*.testadapter.dll", SearchOption.TopDirectoryOnly);

            foreach (var extension in extensions)
            {
                Console.WriteLine("Found extension {0}", Path.GetFileName(extension));
            }
            console.InitializeExtensions(extensions);

            var tests = await console.DiscoverTestsAsync(pathToAssembly, GetRunSettingsXml(targetFrameworkVersion)).ConfigureAwait(false);

            Console.WriteLine("Discovered {0} tests", tests.Count());
            foreach (var test in tests)
            {
                Console.WriteLine("  - {0}", test.FullyQualifiedName);
            }

            console.EndSession();
            return(0);
        }
Пример #6
0
        /**********************************************************************/
        #region Constructors

        public ExplorerVM()
        {
            _testPoints         = new ObservableCollection <ITestPointVM>();
            _readOnlyTestPoints = new ReadOnlyObservableCollection <ITestPointVM>(_testPoints);

            RunAllCommand  = new DelegateCommand(x => System.Windows.MessageBox.Show("TestVM.RunAllCommand.Execute()"));
            RefreshCommand = new DelegateCommand(x => DiscoverTests());

            _sources = new List <string>()
            {
                @"C:\Users\Jake\Documents\Visual Studio 2017\Projects\FaultDictionaryDebugger\CommonUtilitiesTests\bin\Debug\CommonUtilitiesTests.dll"
            };

            _adapters = new List <string>()
            {
                @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\NodejsTools\NodejsTools\Microsoft.NodejsTools.TestAdapter.dll",
                @"C:\Users\Jake\Documents\Visual Studio 2017\Projects\nunit3-vs-adapter\src\NUnitTestAdapter\bin\Debug\net35\NUnit3.TestAdapter.dll"
            };

            if (!Directory.Exists("log"))
            {
                Directory.CreateDirectory("log");
            }
            if (File.Exists("log\vstest.log"))
            {
                File.Delete("log\vstest.log");
            }

            _vstest = new VsTestConsoleWrapper(_vstestPath, new ConsoleParameters()
            {
                LogFilePath = @"log\vstest.log"
            });
            Task.Factory.StartNew(() =>
            {
                _vstest.StartSession();
                _vstest.InitializeExtensions(_adapters);
            }).ContinueWith(task =>
            {
                _vstestInitialized = (task.Status == TaskStatus.RanToCompletion);
            }, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());

            _testDiscoveryManager = new TestDiscoveryManager(_vstest);
            _testDiscoveryManager.Sources.AddRange(_sources);
            _testDiscoveryManager.TestsAdded            += OnTestsDiscovered;
            _testDiscoveryManager.TestDiscoveryComplete += OnTestDiscoveryComplete;


            BreakCommand = new DelegateCommand(x => Break());
        }
Пример #7
0
        /// <summary>
        /// Returns the VsTestConsole Wrapper.
        /// </summary>
        /// <returns></returns>
        public IVsTestConsoleWrapper GetVsTestConsoleWrapper()
        {
            var logFileName = Path.GetFileName(Path.GetTempFileName());
            var logFileDir  = Path.Combine(Path.GetTempPath(), "VSTestConsoleWrapperLogs");

            if (Directory.Exists(logFileDir) == false)
            {
                Directory.CreateDirectory(logFileDir);
            }

            var logFilePath = Path.Combine(logFileDir, logFileName);

            Console.WriteLine($"Logging diagnostics in {logFilePath}");

            string consoleRunnerPath;

            if (this.IsNetCoreRunner())
            {
                consoleRunnerPath = Path.Combine(this.testEnvironment.PublishDirectory, "vstest.console.dll");
            }
            else
            {
                consoleRunnerPath = this.GetConsoleRunnerPath();
            }
            var executablePath = IsWindows ? @"dotnet\dotnet.exe" : @"dotnet-linux/dotnet";
            var dotnetPath     = Path.Combine(this.testEnvironment.ToolsDirectory, executablePath);

            if (!File.Exists(dotnetPath))
            {
                throw new FileNotFoundException($"File '{dotnetPath}' was not found.");
            }

            var vstestConsoleWrapper = new VsTestConsoleWrapper(consoleRunnerPath, dotnetPath, new ConsoleParameters()
            {
                LogFilePath = logFilePath
            });

            vstestConsoleWrapper.StartSession();

            return(vstestConsoleWrapper);
        }
Пример #8
0
        static void Main(string[] args)
        {
            // Path to your test dll
            string testProject = @"C:\Projects\UnitTestProject1\bin\Debug\UnitTestProject1.dll";
            string runsettings = @"<?xml version=""1.0"" encoding=""utf-8""?>
                                             <RunSettings>
                                               <RunConfiguration>
                                               </RunConfiguration>
                                             </RunSettings>";

            VsTestConsoleWrapper wrapper = new VsTestConsoleWrapper(@"{path to your vstest.console.exe}");
            var handler = new RunEventsHandler();

            Console.WriteLine("Started");
            wrapper.StartSession();
            wrapper.RunTests(new List <string>()
            {
                testProject
            }, runsettings, handler);
            Console.WriteLine("Finished");
            Console.ReadKey();
        }
Пример #9
0
        public static TestConsole Create(string pathToVSTest, string pathToAssemblyDirectory, bool debug)
        {
            var resolver          = ResolveAssemblies(pathToVSTest);
            var vstestConsolePath = Path.Combine(pathToVSTest, "vstest.console.exe");

            var parameters = new ConsoleParameters();

            if (debug)
            {
                parameters.LogFilePath = Path.Combine(Path.GetTempPath(), $"VSTestAdapter_{DateTime.Now:yyyyMMdd-HHmmss}.log");
            }

            var console = new VsTestConsoleWrapper(vstestConsolePath, parameters);

            console.StartSession();

            var extensions = Directory.GetFiles(pathToAssemblyDirectory, "*.testadapter.dll", SearchOption.TopDirectoryOnly);

            console.InitializeExtensions(extensions);

            return(new TestConsole(console, resolver));
        }
Пример #10
0
        private void EnsureInitialized(String[] testAdapterPaths)
        {
            if (this.isInitialized)
            {
                return;
            }

            try
            {
                VsTestRunnerSemaphore.WaitOne();

                this.logger.WriteLine("Starting vstest...");

                this.vstest = new VsTestConsoleWrapper(this.vstestConsolePath);
                this.vstest.StartSession();
                this.isInitialized = true;
            }
            finally
            {
                VsTestRunnerSemaphore.Release();
            }
        }
Пример #11
0
        /// <summary>
        /// Returns the VsTestConsole Wrapper.
        /// </summary>
        /// <returns></returns>
        public IVsTestConsoleWrapper GetVsTestConsoleWrapper()
        {
            var logFileName = Path.GetFileName(Path.GetTempFileName());
            var logFileDir  = Path.Combine(Path.GetTempPath(), "VSTestConsoleWrapperLogs");

            if (Directory.Exists(logFileDir) == false)
            {
                Directory.CreateDirectory(logFileDir);
            }

            var logFilePath = Path.Combine(logFileDir, logFileName);

            Console.WriteLine($"Logging diagnostics in {logFilePath}");

            var vstestConsoleWrapper = new VsTestConsoleWrapper(this.GetConsoleRunnerPath(), new ConsoleParameters()
            {
                LogFilePath = logFilePath
            });

            vstestConsoleWrapper.StartSession();

            return(vstestConsoleWrapper);
        }
Пример #12
0
 public CLITestBase()
 {
     vsTestConsoleWrapper = new VsTestConsoleWrapper(this.GetConsoleRunnerPath());
     vsTestConsoleWrapper.StartSession();
 }
Пример #13
0
        public static int Main(string[] args)
        {
            if (args == null || args.Length < 1)
            {
                Console.WriteLine(@"Please provide appropriate arguments. Arguments can be passed as following:");
                Console.WriteLine(@"Microsoft.TestPlatform.TranslationLayer.E2ETest.exe --runner:'[vstest.console path]' --testassembly:'[assemblyPath]' --testadapterpath:'[path]'");
                Console.WriteLine(@"Example: Microsoft.TestPlatform.TranslationLayer.E2ETest.exe --runner:'c:\tmp\vstest.console.dll' --testassembly:'c:\a\a.tests.dll' --testadapterpath:'c:\a\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll'");

                return(1);
            }

            string runnerLocation  = string.Empty;
            string testadapterPath = string.Empty;
            string testAssembly    = string.Empty;

            var separator = new char[] { ':' };

            foreach (var arg in args)
            {
                if (arg.StartsWith("-p:") || arg.StartsWith("--testadapterpath:"))
                {
                    testadapterPath = arg.Split(separator, 2)[1];
                }
                else if (arg.StartsWith("-a:") || arg.StartsWith("--testassembly:"))
                {
                    testAssembly = arg.Split(separator, 2)[1];
                }
                else if (arg.StartsWith("-r:") || arg.StartsWith("--runner:"))
                {
                    runnerLocation = arg.Split(separator, 2)[1];
                }
            }

            Console.WriteLine("Parameters:");
            Console.WriteLine("Runner Path: " + runnerLocation);
            Console.WriteLine("Test Assembly Path: " + testAssembly);
            Console.WriteLine("Test Adapter Path: " + testadapterPath);
            Console.WriteLine("-------------------------------------------------------");

            var logFilePath = Path.Combine(Directory.GetCurrentDirectory(), @"log.txt");
            IVsTestConsoleWrapper consoleWrapper = new VsTestConsoleWrapper(runnerLocation, new ConsoleParameters {
                LogFilePath = logFilePath
            });

            consoleWrapper.StartSession();
            consoleWrapper.InitializeExtensions(new List <string>()
            {
                testadapterPath
            });

            var testCases = DiscoverTests(new List <string>()
            {
                testAssembly
            }, consoleWrapper);

            Console.WriteLine("Discovered Tests Count: " + testCases?.Count());
            Console.WriteLine("Discovered Test: " + testCases?.FirstOrDefault()?.DisplayName);

            Console.WriteLine("-------------------------------------------------------");

            var testresults = RunSelectedTests(consoleWrapper, testCases);

            Console.WriteLine("Run Selected Tests Count: " + testresults?.Count());
            Console.WriteLine("Run Selected Test Result: " + testresults?.FirstOrDefault()?.TestCase?.DisplayName + " :" + testresults?.FirstOrDefault()?.Outcome);

            Console.WriteLine("-------------------------------------------------------");

            testresults = RunAllTests(consoleWrapper, new List <string>()
            {
                testAssembly
            });

            Console.WriteLine("Run All Test Count: " + testresults?.Count());
            Console.WriteLine("Run All Test Result: " + testresults?.FirstOrDefault()?.TestCase?.DisplayName + " :" + testresults?.FirstOrDefault()?.Outcome);

            Console.WriteLine("-------------------------------------------------------");

            testresults = RunTestsWithCustomTestHostLauncher(consoleWrapper, new List <string>()
            {
                testAssembly
            });

            Console.WriteLine("Run All (custom launcher) Test Count: " + testresults?.Count());
            Console.WriteLine("Run All (custom launcher) Test Result: " + testresults?.FirstOrDefault()?.TestCase?.DisplayName + " :" + testresults?.FirstOrDefault()?.Outcome);

            Console.WriteLine("-------------------------------------------------------");

            testresults = RunAllTestsWithTestCaseFilter(consoleWrapper, new List <string>()
            {
                testAssembly
            });

            Console.WriteLine("Run All Test Count: " + testresults?.Count());
            Console.WriteLine("Run All Test Result: " + testresults?.FirstOrDefault()?.TestCase?.DisplayName + " :" + testresults?.FirstOrDefault()?.Outcome);
            Console.WriteLine("-------------------------------------------------------");

            return(0);
        }
Пример #14
0
        static async Task Main()
        {
            // in the next 60 lines I am just building the project and resolving the vstest console
            // this is specific to this approach, and is not mandatory to be done this way,
            // I am just trying to make the project portable
            Console.WriteLine("Running dotnet --version");
            var dotnetVersion = RunCommand("dotnet", "--version").Trim();

            Console.WriteLine(dotnetVersion);
            Console.WriteLine("Running dotnet --list-sdks");
            var sdks = RunCommand("dotnet", "--list-sdks")
                       .Split(Environment.NewLine)
                       .Select(line => line.Trim())
                       .Where(line => !string.IsNullOrWhiteSpace(line))
                       .ToList();

            sdks.ForEach(Console.WriteLine);

            var currentSdk = sdks.First(line => line.StartsWith(dotnetVersion));
            var sdkPath    = currentSdk.Replace($"{dotnetVersion} [", "").TrimEnd(']');

            Console.WriteLine($"Sdk path is: {sdkPath}");

            var vstestConsolePath = Path.Combine(sdkPath, dotnetVersion, "vstest.console.dll");

            Console.WriteLine($"Test console path is: {vstestConsolePath}");


            Console.WriteLine($"Finding test project.");
            var here            = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var testProjectName = "TestProject1";
            var testProjectPath = Path.GetFullPath(Path.Combine(here, "..", "..", "..", "..", testProjectName, $"{testProjectName}.csproj"));

            Console.WriteLine($"Building test project in path: {testProjectPath}");
            var buildOutput = RunCommand("dotnet", $"build {testProjectPath}");

            Console.WriteLine(buildOutput);

            var dllpath = buildOutput.Split(Environment.NewLine)
                          .Select(line => line.Trim())
                          .First(line => line.EndsWith($"{testProjectName}.dll"))
                          .Replace($"{testProjectName} ->", "")
                          .Trim();

            Console.WriteLine($"Test project dll: {dllpath}");

            var logPath = Path.Combine(here, "logs", "log.txt");

            Directory.CreateDirectory(Path.GetDirectoryName(logPath));
            var consoleParameters = new ConsoleParameters {
                LogFilePath = logPath
            };

            // make the timeout shorter to see errors faster in case we do something wrong
            // and the runner won't start, you can look at the logs after the run, or use
            // DebugView++ to see them in real time
            Environment.SetEnvironmentVariable("VSTEST_CONNECTION_TIMEOUT", "20");

            // THIS is where the interesting code starts :) We make a console wrapper,
            // and point it at our console path, this can be either vstest.console.exe
            // if we are taking it from VS installation, or from Microsoft.TestPlaform
            // nuget package,
            // or vstest.console.dll if we are using the one from dotnet sdk.
            var wrapper = new VsTestConsoleWrapper(vstestConsolePath, consoleParameters);

            Console.WriteLine($"Discovering tests.");
            var discoveryHandler = new DiscoveryHandler();

            // Discover all tests from the assembly.
            // Make sure you don't provide null for the runsettings.
            // Make sure your dll paths are not sorrounded by whitespace.
            // Use the sync api.
            wrapper.DiscoverTests(new[] { dllpath }, "", discoveryHandler);

            var tests = discoveryHandler.DiscoveredTests;

            Console.WriteLine($"Found {tests.Count} tests.");


            var half = tests.Count / 2;

            // split them to two batches
            var firstHalf  = tests.Take(half).ToList();
            var secondHalf = tests.Skip(half).ToList();

            var run1Handler = new RunHandler();
            var run2Handler = new RunHandler();

            // Running each batch
            // Make sure you provide provide at least the root tag for the runsettings.
            wrapper.RunTests(firstHalf, "<RunSettings></RunSettings>", run1Handler);
            Console.WriteLine("First half:");
            run1Handler.TestResults.ForEach(WriteTestResult);

            wrapper.RunTests(secondHalf, "<RunSettings></RunSettings>", run2Handler);
            Console.WriteLine("Second half:");
            run2Handler.TestResults.ForEach(WriteTestResult);

            // Trying it with async
            run1Handler.TestResults.Clear();
            run2Handler.TestResults.Clear();

            // Make sure you provide provide at least the root tag for the runsettings.
            var run1 = wrapper.RunTestsAsync(firstHalf, "<RunSettings></RunSettings>", run1Handler);
            // there is a bug that will report using one of the handlers when the requests come too close together
            // this won't happen for third request. BUT it should not matter to you if you use the same handler for all
            // batches as it is usual.
            var run2 = wrapper.RunTestsAsync(secondHalf, "<RunSettings></RunSettings>", run2Handler);
            await Task.WhenAll(run1, run2);


            Console.WriteLine("First half async:");
            run1Handler.TestResults.ForEach(WriteTestResult);
            Console.WriteLine("Second half async:");
            run2Handler.TestResults.ForEach(WriteTestResult);

            Console.WriteLine("Done.");
            Console.ReadLine();
        }