Perf Test fixing bugs and Update command (#155)

* Fixed some bugs caused by rafactoring

* Verifying a test db is created before running the tests
This commit is contained in:
Leila Lali
2016-11-30 12:56:48 -08:00
committed by GitHub
parent d1b791805a
commit 453ff9de15
19 changed files with 708 additions and 804 deletions

View File

@@ -41,6 +41,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
}
catch (Exception ex)
{
Console.WriteLine("Failed to load the database connection server name settings. error: " + ex.Message);
return null;
}
}
@@ -56,6 +57,7 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
}
catch (Exception ex)
{
Console.WriteLine("Failed to load the connection settings. error: " + ex.Message);
return null;
}
}

View File

@@ -4,10 +4,12 @@
//
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
using Xunit.Sdk;
namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
{
@@ -23,27 +25,33 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
bool containsTestName = testName.Contains(".");
var className = containsTestName ? testName.Substring(0, testName.LastIndexOf('.')) : testName;
var methodName = containsTestName ? testName.Substring(testName.LastIndexOf('.') + 1) : null;
var type = Type.GetType(testNamespace + className);
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(type, method, method.Name);
await RunTest(typeInstance, method, method.Name);
}
}
else
{
MethodInfo methodInfo = type.GetMethod(methodName);
await RunTest(type, methodInfo, test);
await RunTest(typeInstance, methodInfo, test);
}
IDisposable disposable = typeInstance as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
@@ -56,21 +64,43 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
return 0;
}
private static async Task RunTest(Type type, MethodBase methodInfo, string testName)
private static async Task RunTest(object typeInstance, MethodInfo methodInfo, string testName)
{
if (methodInfo == null)
try
{
Console.WriteLine("Invalid method name");
}
else
{
using (var typeInstance = (IDisposable)Activator.CreateInstance(type))
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));
}
}
}
}

View File

@@ -17,7 +17,6 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
{
private static string resultFolder = InitResultFolder();
private static string InitResultFolder()
{
string resultFodler = Environment.GetEnvironmentVariable("ResultFolder");
@@ -34,6 +33,8 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
Start();
}
public bool PrintResult { get; set; }
public void Start()
{
StartDateTime = DateTime.UtcNow;
@@ -47,15 +48,18 @@ namespace Microsoft.SqlTools.ServiceLayer.TestDriver.Utility
public void EndAndPrint([CallerMemberName] string testName = "")
{
End();
var currentColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Test Name: {0} Run time in milliSeconds: {1}", testName, TotalMilliSeconds));
Console.ForegroundColor = currentColor;
string resultContent = Newtonsoft.Json.JsonConvert.SerializeObject(new TestResult { ElapsedTime = TotalMilliSeconds });
string fileName = testName + ".json";
string resultFilePath = string.IsNullOrEmpty(resultFolder) ? fileName : Path.Combine(resultFolder, fileName);
File.WriteAllText(resultFilePath, resultContent);
Console.WriteLine("Result file: " + resultFilePath);
if (PrintResult)
{
var currentColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Test Name: {0} Run time in milliSeconds: {1}", testName, TotalMilliSeconds));
Console.ForegroundColor = currentColor;
string resultContent = Newtonsoft.Json.JsonConvert.SerializeObject(new TestResult { ElapsedTime = TotalMilliSeconds });
string fileName = testName + ".json";
string resultFilePath = string.IsNullOrEmpty(resultFolder) ? fileName : Path.Combine(resultFolder, fileName);
File.WriteAllText(resultFilePath, resultContent);
Console.WriteLine("Result file: " + resultFilePath);
}
}
public double TotalMilliSeconds