public override TestResult Execute(TestExecutionContext context) { TestResult testResults = null; try { command.Execute(context); } catch (Exception ex) { String msg = ex.Message; } testResults = context.CurrentResult; if (context.CurrentResult.FailCount > 0) { context.CurrentResult.SetResult(ResultState.Failure); } else { context.CurrentResult.SetResult(ResultState.Success); } TestMethodDataEntity testMethodDataEntity = new TestMethodDataEntity(); testMethodDataEntity.TestMethodName = context.CurrentTest.MethodName; testMethodDataEntity.TestClassName = context.CurrentTest.ClassName; testMethodDataEntity.TestDataReference = JsonConvert.SerializeObject(context.CurrentTest.Arguments); testMethodDataEntity.Result = (context.CurrentResult.ResultState == ResultState.Success) ? true : false; String message = ""; if (!testMethodDataEntity.Result) { for (int i = 0; i < context.CurrentResult.AssertionResults.Count; i++) { message += context.CurrentResult.AssertionResults[i].Message; } testMethodDataEntity.Message = message; // TODO: Buidling custom error messages // Classname, methodname and argument list is available. // Based on the argument list get the expected response object // Get the actual response object --------------------------------- NEED TO CHECK THIS // Get all the properties via reflection // Compare and build a custom error message } MongoDbConnection mongoDbConnection = new MongoDbConnection(); mongoDbConnection.upsertTestResultsData(testMethodDataEntity); return(testResults); }
public static void upsertTestMethodData(TestMethodDataEntity testMethodDataEntity) { string connstring = "Server=127.0.0.1; Port=5432; User Id=postgres; Password=admin; Database=TestFramework;"; NpgsqlConnection connection = null; try { String timeStamp = (DateTime.Now).ToString("yyyy/MM/dd HH:mm:ss"); connection = new NpgsqlConnection(connstring); connection.Open(); String selectQuery = "select COUNT(*) from testmethoddata where testclassname = :testclassname and testmethodname = :testmethodname and testdatareference = :testdatareference"; NpgsqlCommand command = new NpgsqlCommand(selectQuery, connection); command.Parameters.Add(new NpgsqlParameter("testclassname", testMethodDataEntity.TestClassName)); command.Parameters.Add(new NpgsqlParameter("testmethodname", testMethodDataEntity.TestMethodName)); command.Parameters.Add(new NpgsqlParameter("testdatareference", testMethodDataEntity.TestDataReference)); Int64 count = (Int64)command.ExecuteScalar(); if (count == 0) { NpgsqlCommand command2 = new NpgsqlCommand("insert into testmethoddata (testmethodname, testclassname, testdatareference, result, message, lastruntimestamp) values(:testmethodname, :testclassname, :testdatareference, :result, :message, :lastruntimestamp)", connection); command2.Parameters.Add(new NpgsqlParameter("testmethodname", testMethodDataEntity.TestMethodName)); command2.Parameters.Add(new NpgsqlParameter("testclassname", testMethodDataEntity.TestClassName)); command2.Parameters.Add(new NpgsqlParameter("testdatareference", testMethodDataEntity.TestDataReference)); command2.Parameters.Add(new NpgsqlParameter("result", testMethodDataEntity.Result)); command2.Parameters.Add(new NpgsqlParameter("lastruntimestamp", timeStamp)); if (!testMethodDataEntity.Result) { command2.Parameters.Add(new NpgsqlParameter("message", testMethodDataEntity.Message)); } else { command2.Parameters.Add(new NpgsqlParameter("message", "")); } command2.ExecuteNonQuery(); } else { NpgsqlCommand command2 = new NpgsqlCommand("update testmethoddata set result = :result, message = :message, lastruntimestamp = :lastruntimestamp where testmethodname = :testmethodname and testclassname= :testclassname and testdatareference = :testdatareference", connection); command2.Parameters.Add(new NpgsqlParameter("testmethodname", testMethodDataEntity.TestMethodName)); command2.Parameters.Add(new NpgsqlParameter("testclassname", testMethodDataEntity.TestClassName)); command2.Parameters.Add(new NpgsqlParameter("testdatareference", testMethodDataEntity.TestDataReference)); command2.Parameters.Add(new NpgsqlParameter("result", testMethodDataEntity.Result)); if (!testMethodDataEntity.Result) { command2.Parameters.Add(new NpgsqlParameter("message", testMethodDataEntity.Message)); } else { command2.Parameters.Add(new NpgsqlParameter("message", "")); } command2.Parameters.Add(new NpgsqlParameter("lastruntimestamp", timeStamp)); command2.ExecuteNonQuery(); } }catch (Exception ex) { String ErrorMessage = ex.Message; } finally { connection.Close(); } }