public static TestResult[] Execute(ITestMethod testMethod) { // Get our test context TestContext testContext = testMethod.GetTestContext(); // Get a possible Description attribute var desc = testMethod.MethodInfo.GetCustomAttribute <DescriptionAttribute>(inherit: true)?.Description; if (string.IsNullOrEmpty(desc)) { desc = testMethod.MethodInfo.GetCustomAttribute <System.ComponentModel.DescriptionAttribute>(inherit: true)?.Description; } string customDisplayName = null; // Test has method arguments, include them in the description if (testMethod.Arguments?.Length > 0) { // Test has description, use it as a string formatter for the arguments if (!string.IsNullOrEmpty(desc)) { try { customDisplayName = testMethod.TestMethodName + " - " + string.Format(CultureInfo.InvariantCulture, desc, testMethod.Arguments); } catch (Exception ex) { // String format with description failed customDisplayName = testMethod.TestMethodName + " - " + ex.Message; } } else { // No description formatter, only append arguments to test name customDisplayName = testMethod.TestMethodName + " - " + string.Join(", ", testMethod.Arguments); } } // Test only has description, use in display name else if (desc != null) { customDisplayName = testMethod.TestMethodName + " - " + desc; } if (!string.IsNullOrEmpty(customDisplayName)) { testContext.GetInternalTestState().CustomDisplayName = customDisplayName; } var executeResult = testMethod.Invoke(testMethod.Arguments); if (!string.IsNullOrEmpty(customDisplayName)) { executeResult.DisplayName = customDisplayName; } return(new[] { executeResult }); }
public override TestResult[] Execute(ITestMethod testMethod) { // Get our test context TestContext testContext = testMethod.GetTestContext(); // Create a new internal test state. InternalTestState internalTestState = testContext.ResetInternalTestState(); // Convert test argument types object[] args = null; if (testMethod.Arguments != null) { args = new object[testMethod.Arguments.Length]; for (var i = 0; i < args.Length; i++) { args[i] = TypeConversion.ConvertValue(testMethod.ParameterTypes[i].ParameterType, testMethod.Arguments[i]); } } // Execute our test method on our main node. TestResult mainResult = testMethod.Invoke(args); // Set a more accurate time elapse duration (end of init to start of cleanup) mainResult.Duration = internalTestState.EndTime - internalTestState.StartTime; // Obtain the display name for our test. string testDisplayName = mainResult.DisplayName ?? testMethod.TestMethodName; // Reset the internal test state for this test context internalTestState = testContext.ResetInternalTestState(); // If we have a parallel node to run tests against.. if (Global.ExternalNodeTestServices != null) { // If we are only running against the external node if (Global.OnlyUseExternalNode.GetValueOrDefault()) { // Only using the external node means the above method invocation already ran on // the external node, so we quit with the singular result. return(new[] { mainResult }); } // Begin using the external node. internalTestState.InExternalNodeContext = true; // Execute our test method on our parallel node TestResult parallelResult = testMethod.Invoke(args); // Set a more accurate time elapse duration (end of init to start of cleanup) parallelResult.Duration = internalTestState.EndTime - internalTestState.StartTime; // Stop using the external node. internalTestState.InExternalNodeContext = false; // Set our test names mainResult.DisplayName = $"{testDisplayName} (built-in)"; parallelResult.DisplayName = $"{testDisplayName} (external)"; return(new[] { mainResult, parallelResult }); } else { var parallelResult = new TestResult { Outcome = UnitTestOutcome.Error, TestFailureException = new Exception("Failed to run parallel test because parallel test node services failed to initialize properly."), }; // Set our test names mainResult.DisplayName = $"{testDisplayName} (built-in)"; parallelResult.DisplayName = $"{testDisplayName} (external)"; return(new[] { mainResult, parallelResult }); } }