mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Convert most tools service tests to nunit (#1037)
* Remove xunit dependency from testdriver * swap expected/actual as needed * Convert Test.Common to nunit * port hosting unit tests to nunit * port batchparser integration tests to nunit * port testdriver.tests to nunit * fix target to copy dependency * port servicelayer unittests to nunit * more unit test fixes * port integration tests to nunit * fix test method type * try using latest windows build for PRs * reduce test memory use
This commit is contained in:
@@ -49,9 +49,14 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Driver
|
||||
string serviceHostArguments = "--enable-logging";
|
||||
if (string.IsNullOrWhiteSpace(serviceHostExecutable))
|
||||
{
|
||||
|
||||
// Include a fallback value to for running tests within visual studio
|
||||
serviceHostExecutable =
|
||||
@"..\..\..\..\..\src\Microsoft.SqlTools.ServiceLayer\bin\Debug\netcoreapp3.1\win7-x64\MicrosoftSqlToolsServiceLayer.exe";
|
||||
if (!File.Exists(serviceHostExecutable))
|
||||
{
|
||||
serviceHostExecutable = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "MicrosoftSqlToolsServiceLayer.exe");
|
||||
}
|
||||
}
|
||||
|
||||
serviceHostExecutable = Path.GetFullPath(serviceHostExecutable);
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../src/Microsoft.SqlTools.Hosting/Microsoft.SqlTools.Hosting.csproj" />
|
||||
|
||||
@@ -4,13 +4,7 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
{
|
||||
@@ -95,90 +89,15 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
|
||||
public async Task<int> RunTests(string[] args, string testNamespace)
|
||||
{
|
||||
ParseArguments(args);
|
||||
foreach (var test in Tests)
|
||||
{
|
||||
try
|
||||
{
|
||||
var testName = test.Contains(testNamespace) ? test.Replace(testNamespace, "") : test;
|
||||
bool containsTestName = testName.Contains(".");
|
||||
var className = containsTestName ? testName.Substring(0, testName.LastIndexOf('.')) : testName;
|
||||
var methodName = containsTestName ? testName.Substring(testName.LastIndexOf('.') + 1) : null;
|
||||
Assembly assembly = Assembly.GetEntryAssembly();
|
||||
Type type = assembly.GetType(testNamespace + className);
|
||||
if (type == null)
|
||||
{
|
||||
Console.WriteLine("Invalid class name");
|
||||
}
|
||||
else
|
||||
{
|
||||
var typeInstance = Activator.CreateInstance(type);
|
||||
if (string.IsNullOrEmpty(methodName))
|
||||
{
|
||||
var methods = type.GetMethods().Where(x => x.CustomAttributes.Any(a => a.AttributeType == typeof(FactAttribute)));
|
||||
foreach (var method in methods)
|
||||
{
|
||||
await RunTest(typeInstance, method, method.Name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MethodInfo methodInfo = type.GetMethod(methodName);
|
||||
await RunTest(typeInstance, methodInfo, test);
|
||||
}
|
||||
|
||||
IDisposable disposable = typeInstance as IDisposable;
|
||||
if (disposable != null)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static async Task RunTest(object typeInstance, MethodInfo methodInfo, string testName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (methodInfo == null)
|
||||
{
|
||||
Console.WriteLine("Invalid method name");
|
||||
}
|
||||
else
|
||||
{
|
||||
var testAttributes = methodInfo.CustomAttributes;
|
||||
BeforeAfterTestAttribute beforeAfterTestAttribute = null;
|
||||
foreach (var attribute in testAttributes)
|
||||
{
|
||||
var args = attribute.ConstructorArguments.Select(x => x.Value as object).ToArray();
|
||||
var objAttribute = Activator.CreateInstance(attribute.AttributeType, args);
|
||||
|
||||
beforeAfterTestAttribute = objAttribute as BeforeAfterTestAttribute;
|
||||
if (beforeAfterTestAttribute != null)
|
||||
{
|
||||
beforeAfterTestAttribute.Before(methodInfo);
|
||||
}
|
||||
}
|
||||
Console.WriteLine("Running test " + testName);
|
||||
await (Task)methodInfo.Invoke(typeInstance, null);
|
||||
if (beforeAfterTestAttribute != null)
|
||||
{
|
||||
beforeAfterTestAttribute.After(methodInfo);
|
||||
}
|
||||
Console.WriteLine("Test ran successfully: " + testName);
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Test Failed: {0} error: {1}", testName, ex.Message));
|
||||
|
||||
}
|
||||
{
|
||||
throw new NotImplementedException("This code needs to change to use 'dotnet test' or nunit directly");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
return await Task.FromResult(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user