/// <summary> /// Construct one or more TestMethods from a given MethodInfo, /// using available parameter data. /// </summary> /// <param name="method">The MethodInfo for which tests are to be constructed.</param> /// <param name="suite">The suite to which the tests will be added.</param> /// <returns>One or more TestMethods</returns> public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite) { List <TestMethod> tests = new List <TestMethod>(); IParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 0) { IEnumerable[] sources = new IEnumerable[parameters.Length]; try { for (int i = 0; i < parameters.Length; i++) { sources[i] = _dataProvider.GetDataFor(parameters[i]); } } catch (InvalidDataSourceException ex) { var parms = new TestCaseParameters(); parms.RunState = RunState.NotRunnable; parms.Properties.Set(PropertyNames.SkipReason, ex.Message); tests.Add(_builder.BuildTestMethod(method, suite, parms)); return(tests); } foreach (var parms in _strategy.GetTestCases(sources)) { tests.Add(_builder.BuildTestMethod(method, suite, (TestCaseParameters)parms)); } } return(tests); }
/// <summary> /// Gets the test cases generated by the CombiningStrategy. /// </summary> /// <returns>The test cases.</returns> public IEnumerable<ITestCaseData> GetTestCases(IEnumerable[] sources) { List<ITestCaseData> testCases = new List<ITestCaseData>(); IEnumerator[] enumerators = new IEnumerator[sources.Length]; int index = -1; for (; ; ) { while (++index < sources.Length) { enumerators[index] = sources[index].GetEnumerator(); if (!enumerators[index].MoveNext()) return testCases; } object[] testdata = new object[sources.Length]; for (int i = 0; i < sources.Length; i++) testdata[i] = enumerators[i].Current; TestCaseParameters parms = new TestCaseParameters(testdata); testCases.Add(parms); index = sources.Length; while (--index >= 0 && !enumerators[index].MoveNext()) ; if (index < 0) break; } return testCases; }
/// <summary> /// Builds any number of tests from the specified method and context. /// </summary> /// <param name="method">The MethodInfo for which tests are to be constructed.</param> /// <param name="suite">The suite to which the tests will be added.</param> public IEnumerable <ITestCaseData> GetTestCasesFor(MethodInfo method) { List <ITestCaseData> data = new List <ITestCaseData>(); var dataProvider = new ParameterDataProvider(method); ParameterInfo[] parameters = method.GetParameters(); var name = method.Name; // For Debugging if (parameters.Length > 0) { IEnumerable[] sources = new IEnumerable[parameters.Length]; try { for (int i = 0; i < parameters.Length; i++) { sources[i] = dataProvider.GetDataFor(parameters[i]); } foreach (var parms in _strategy.GetTestCases(sources)) { data.Add(parms); } } catch (Exception ex) { var parms = new TestCaseParameters(ex); data.Add(parms); } } return(data); }
/// <summary> /// Simulate the test case. If service is available, execute the real test and return the output, /// otherwise return the mock output. /// If “UseMockTestCase” is ‘true’ /// Get the mock result (object) from “MockOutputRepository”. /// Create an instance of “TestCaseOutput” with mock result (object). /// Return instance of “TestCaseOutput”. /// Else /// Execute the real world function pointer and get the result (object). /// If execution fails (On exception. Only WebException & SoapException?) /// Increment “_RealTestCaseFailureCount” /// If “_RealTestCaseFailureCount” > “_MaximumRealTestCaseFailure”, set _UseMockTestCase to true. /// Get the mock result (object) from “MockOutputRepository”. /// Create an instance of “TestCaseOutput” with output result (object). /// Return instance of “TestCaseOutput”. /// </summary> /// <param name="parameters">Test case parameters</param> public TestCaseOutput Simulate(TestCaseParameters parameters) { if (!UseMockTestCase) { try { return(parameters.CallbackMethod(parameters.CallbackParameters)); } catch { // Ignore the exception _RealTestCaseFailureCount++; if (_RealTestCaseFailureCount == _MaximumRealTestCaseFailure) { _UseMockTestCase = true; } } } return(new TestCaseOutput( MockOutputRepository.Instance.GetOutput(parameters.TestCaseId), true)); }
public static double[] TrainRBFodel(MachineLearningController controller, TestCaseParameters param, List <double> outputToTest = null, int outputSize = 0) { MachineLearningTestCase.lastTestCaseParameters = param; if (param.neuronsPerLayer[0] < 3) { controller.InstantiateSpheresInScene(param.X, param.sampleSize, null); } int eachOutputSize = param.neuronsPerLayer[param.neuronsPerLayer.Count - 1]; List <double> outputTest = (outputToTest == null || outputToTest.Count == 0) ? param.X : outputToTest; int outputTotalSize = (outputToTest == null || outputToTest.Count == 0) ? param.sampleSize * eachOutputSize: outputSize; IntPtr rawResut; rawResut = CppImporter.trainRBFModel( param.X.ToArray(), param.sampleSize, param.neuronsPerLayer[0], param.Y.ToArray(), param.nbCentroids, param.nbClasses, param.maxKmeans, outputTest.ToArray(), param.Y.ToArray(), param.gamma, outputTotalSize); double[] result = new double[outputTotalSize]; Marshal.Copy(rawResut, result, 0, outputTotalSize); Debug.Log("Result rbf "); return(result); }
/// <summary> /// Simulate the test case. If service is available, execute the real test and return the output, /// otherwise return the mock output. /// If “UseMockTestCase” is ‘true’ /// Get the mock result (object) from “MockOutputRepository”. /// Create an instance of “TestCaseOutput” with mock result (object). /// Return instance of “TestCaseOutput”. /// Else /// Execute the real world function pointer and get the result (object). /// If execution fails (On exception. Only WebException & SoapException?) /// Increment “_RealTestCaseFailureCount” /// If “_RealTestCaseFailureCount” > “_MaximumRealTestCaseFailure”, set _UseMockTestCase to true. /// Get the mock result (object) from “MockOutputRepository”. /// Create an instance of “TestCaseOutput” with output result (object). /// Return instance of “TestCaseOutput”. /// </summary> /// <param name="parameters">Test case parameters</param> public TestCaseOutput Simulate(TestCaseParameters parameters) { if (!UseMockTestCase) { try { return parameters.CallbackMethod(parameters.CallbackParameters); } catch { // Ignore the exception _RealTestCaseFailureCount++; if (_RealTestCaseFailureCount == _MaximumRealTestCaseFailure) { _UseMockTestCase = true; } } } return new TestCaseOutput( MockOutputRepository.Instance.GetOutput(parameters.TestCaseId), true); }
/// <summary> /// Gets the test cases generated by the CombiningStrategy. /// </summary> /// <returns>The test cases.</returns> public IEnumerable<ITestCaseData> GetTestCases(IEnumerable[] sources) { List<ITestCaseData> testCases = new List<ITestCaseData>(); IEnumerator[] enumerators = new IEnumerator[sources.Length]; for (int i = 0; i < sources.Length; i++) enumerators[i] = sources[i].GetEnumerator(); for (; ; ) { bool gotData = false; object[] testdata = new object[sources.Length]; for (int i = 0; i < sources.Length; i++) if (enumerators[i].MoveNext()) { testdata[i] = enumerators[i].Current; gotData = true; } else testdata[i] = null; if (!gotData) break; TestCaseParameters parms = new TestCaseParameters(testdata); testCases.Add(parms); } return testCases; }
private static IEnumerable SourceMustBeStaticError() { var parms = new TestCaseParameters(); parms.RunState = RunState.NotRunnable; parms.Properties.Set(PropertyNames.SkipReason, "The sourceName specified on a TestCaseSourceAttribute must refer to a static field, property or method."); return(new TestCaseParameters[] { parms }); }
private static IEnumerable ReturnErrorAsParameter(string errorMessage) { var parms = new TestCaseParameters(); parms.RunState = RunState.NotRunnable; parms.Properties.Set(PropertyNames.SkipReason, errorMessage); return(new TestCaseParameters[] { parms }); }
/// <summary> /// Builds a single TestMethod /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parentSuite">The suite or fixture to which the new test will be added</param> /// <param name="parameterSet">The ParameterSet to be used, or null</param> /// <returns></returns> private TestMethod BuildTestMethod(MethodInfo method, ITest parentSuite, TestCaseParameters parameterSet) { TestMethod testMethod = new TestMethod(method, parentSuite); testMethod.Seed = _randomizer.Next(); string prefix = method.ReflectedType.FullName; // Needed to give proper fullname to test in a parameterized fixture. // Without this, the arguments to the fixture are not included. if (parentSuite != null) { prefix = parentSuite.FullName; //testMethod.FullName = prefix + "." + testMethod.Name; } if (CheckTestMethodSignature(testMethod, parameterSet)) { testMethod.ApplyAttributesToTest(method.ReflectedType); testMethod.ApplyAttributesToTest(method); foreach (ICommandWrapper decorator in method.GetCustomAttributes(typeof(ICommandWrapper), true)) { testMethod.CustomDecorators.Add(decorator); } } // NOTE: In the case of a generic method, testMethod.Method // may be changed in the call to CheckTestMethodSignature. // This is just -in case some future change tries to use it. method = testMethod.Method; if (parameterSet != null) { if (parameterSet.TestName != null) { testMethod.Name = parameterSet.TestName; testMethod.FullName = prefix + "." + parameterSet.TestName; } else if (parameterSet.OriginalArguments != null) { string name = MethodHelper.GetDisplayName(method, parameterSet.OriginalArguments); testMethod.Name = name; testMethod.FullName = prefix + "." + name; } parameterSet.ApplyToTest(testMethod); } if (parentSuite.RunState == RunState.NotRunnable || parentSuite.RunState == RunState.Skipped) { testMethod.RunState = parentSuite.RunState; var reasonKey = PropertyNames.SkipReason; testMethod.Properties.Set(reasonKey, parentSuite.Properties.Get(reasonKey)); } return(testMethod); }
/// <summary> /// Gets the test cases generated by the CombiningStrategy. /// </summary> /// <returns>The test cases.</returns> public IEnumerable <ITestCaseData> GetTestCases(IEnumerable[] sources) { var testCases = new List <ITestCaseData>(); var enumerators = new IEnumerator[sources.Length]; for (int i = 0; i < sources.Length; i++) { enumerators[i] = sources[i].GetEnumerator(); } bool reachedEndOfOneEnumerator = false; while (true) { object[] testdata = new object[sources.Length]; bool reachedEndOfAllEnumerators = true; for (int i = 0; i < sources.Length; i++) { if (enumerators[i].MoveNext()) { testdata[i] = enumerators[i].Current; reachedEndOfAllEnumerators = false; } else { if (StopAtShortestSource) { reachedEndOfOneEnumerator = true; break; } else { testdata[i] = null; } } } if (StopAtShortestSource && !reachedEndOfOneEnumerator) { var parms = new TestCaseParameters(testdata); testCases.Add(parms); } else { if (reachedEndOfAllEnumerators) { break; } var parms = new TestCaseParameters(testdata); testCases.Add(parms); } } return(testCases); }
/// <summary> /// Builds a single NUnitTestMethod, either as a child of the fixture /// or as one of a set of test cases under a ParameterizedTestMethodSuite. /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parentSuite">The suite or fixture to which the new test will be added</param> /// <param name="parms">The ParameterSet to be used, or null</param> public TestMethod BuildTestMethod(IMethodInfo method, Test parentSuite, TestCaseParameters parms) { var testMethod = new TestMethod(method, parentSuite) { Seed = _randomizer.Next() }; CheckTestMethodAttributes(testMethod); CheckTestMethodSignature(testMethod, parms); if (parms == null || parms.Arguments == null) { testMethod.ApplyAttributesToTest(method.MethodInfo); } // NOTE: After the call to CheckTestMethodSignature, the Method // property of testMethod may no longer be the same as the // original MethodInfo, so we don't use it here. string prefix = testMethod.Method.TypeInfo.FullName; // Needed to give proper full name to test in a parameterized fixture. // Without this, the arguments to the fixture are not included. if (parentSuite != null) { prefix = parentSuite.FullName; } if (parms != null) { parms.ApplyToTest(testMethod); if (parms.TestName != null) { // The test is simply for efficiency testMethod.Name = parms.TestName.Contains("{") ? new TestNameGenerator(parms.TestName).GetDisplayName(testMethod, parms.OriginalArguments) : parms.TestName; } else if (parms.ArgDisplayNames != null) { testMethod.Name = testMethod.Name + '(' + string.Join(", ", parms.ArgDisplayNames) + ')'; } else { testMethod.Name = _nameGenerator.GetDisplayName(testMethod, parms.OriginalArguments); } } else { testMethod.Name = _nameGenerator.GetDisplayName(testMethod, null); } testMethod.FullName = prefix + "." + testMethod.Name; return(testMethod); }
public void SimulateResultTest() { simulateTestCaseParameters = MachineLearningTestCase.GetTestOption(testCaseOption, algoUsed); MachineLearningTestCase.lastTestCaseParameters = simulateTestCaseParameters; if (testCaseOption != TestCaseOption.IMAGES_TEST && testCaseOption != TestCaseOption.USE_IMAGES) { InstantiateSpheresInScene(simulateTestCaseParameters.X, simulateTestCaseParameters.sampleSize, simulateTestCaseParameters.Y); } }
internal TestCaseParameters GetTestPamameters(int i, InitializationLock locker) { var behaviour = new BehaviourExecutor(i, locker); var parms = new TestCaseParameters(new[] { behaviour }) { TestName = Name(i) }; return(parms); }
/// <summary> /// Validate Cancelling submitted Job. /// Input Data :Valid search query, Database value and program value. /// Output Data : Validation of Cancelling submitted Job. /// </summary> /// Invalidated the test case for the Bug 115 public void ValidateCancelSubmittedJob() { // Gets the search query parameter and their values. string querySequence = _utilityObj._xmlUtil.GetTextValue( Constants.AzureBlastResultsNode, Constants.QuerySequency); string queryDatabaseValue = _utilityObj._xmlUtil.GetTextValue( Constants.AzureBlastResultsNode, Constants.DatabaseValue); string queryProgramValue = _utilityObj._xmlUtil.GetTextValue( Constants.AzureBlastResultsNode, Constants.ProgramValue); string queryDatabaseParameter = _utilityObj._xmlUtil.GetTextValue( Constants.AzureBlastResultsNode, Constants.DatabaseParameter); string queryProgramParameter = _utilityObj._xmlUtil.GetTextValue( Constants.AzureBlastResultsNode, Constants.ProgramParameter); // Set Blast Parameters IBlastServiceHandler service = null; try { service = new AzureBlastHandler(); BlastParameters queryParams = new BlastParameters(); queryParams.Add(queryDatabaseParameter, queryDatabaseValue); queryParams.Add(queryProgramParameter, queryProgramValue); Dictionary <string, object> testCaseParms = new Dictionary <string, object>(); testCaseParms.Add(Constants.BlastParmsConst, queryParams); testCaseParms.Add(Constants.QuerySeqString, querySequence); // Get request identifier TestCaseParameters parameters = new TestCaseParameters( Constants.AzureWebServiceCancelSubmitRequestTest, null, GetRequestIdentifier, testCaseParms); object resultsObject = _TestCaseSimulator.Simulate(parameters).Result; // Cancel subitted job. bool result = service.CancelRequest(resultsObject as string); // validate the cancelled job. Assert.IsTrue(result); Console.WriteLine(string.Format(null, "Azure Blast BVT : Submitted job cancelled was successfully.", queryProgramValue)); } finally { if (service != null) { ((IDisposable)service).Dispose(); } } }
TestMethod CreateTestMethod(IMethodInfo method, Test suite) { TestCaseParameters parms = new TestCaseParameters { ExpectedResult = new object(), HasExpectedResult = true }; var t = _builder.BuildTestMethod(method, suite, parms); return(t); }
public TestMethod BuildFrom(IMethodInfo method, NUnit.Framework.Internal.Test suite) { var defaultParameters = method.GetParameters().Select( parameter => TypeUtils.defaultOf(parameter.ParameterType)).ToArray(); var parameters = new TestCaseParameters(defaultParameters); if (method.ReturnType.Type != typeof(void)) { parameters.ExpectedResult = null; } return(_builder.BuildTestMethod(method, suite, parameters)); }
/// <summary> /// Builds a single test from the specified method and context. /// </summary> /// <param name="method">The method for which a test is to be constructed.</param> /// <param name="suite">The suite to which the test will be added.</param> public TestMethod BuildFrom(IMethodInfo method, Test suite) { TestCaseParameters parms = null; if (_hasExpectedResult) { parms = new TestCaseParameters(); parms.ExpectedResult = ExpectedResult; } return(_builder.BuildTestMethod(method, suite, parms)); }
/// <summary> /// Before NUnit 3.5 the Arguments and OriginalArguments properties are referencing the same array, so /// we cannot safely update the OriginalArguments without touching the Arguments value. /// This method fixes that by making the OriginalArguments array a standalone copy. /// <para> /// When running in NUnit3.5 and later the method is supposed to do nothing. /// </para> /// </summary> private static void EnsureOriginalArgumentsArrayIsNotShared(TestCaseParameters parameters) { if (ReferenceEquals(parameters.Arguments, parameters.OriginalArguments)) { var clonedArguments = new object[parameters.OriginalArguments.Length]; Array.Copy(parameters.OriginalArguments, clonedArguments, parameters.OriginalArguments.Length); // Unfortunately the property has a private setter, so can be updated via reflection only. // Should use the type where the property is declared as otherwise the private setter is not available. var property = typeof(TestParameters).GetTypeInfo().GetProperty(nameof(TestCaseParameters.OriginalArguments)); property.SetValue(parameters, clonedArguments, null); } }
/// <summary> /// Build a Test from the provided MethodInfo. Depending on /// whether the method takes arguments and on the availability /// of test case data, this method may return a single test /// or a group of tests contained in a ParameterizedMethodSuite. /// </summary> /// <param name="method">The MethodInfo for which a test is to be built</param> /// <param name="parentSuite">The test fixture being populated, or null</param> /// <returns>A Test representing one or more method invocations</returns> public IEnumerable <Test> BuildFrom(MethodInfo method, ITest parentSuite) { List <TestMethod> testCases = new List <TestMethod>(); var name = method.Name; // For Debugging List <ITestCaseFactory> sources = new List <ITestCaseFactory>( (ITestCaseFactory[])method.GetCustomAttributes(typeof(ITestCaseFactory), false)); // See if we need to add a CombinatorialAttribute for parameterized data var parameters = method.GetParameters(); bool hasParamData = parameters.Any(param => param.IsDefined(typeof(IParameterDataSource), false)); if (hasParamData) { bool hasStrategy = sources.Any(source => source is CombiningStrategyAttribute); if (!hasStrategy) { sources.Add(new CombinatorialAttribute()); } } foreach (ITestCaseFactory source in sources) { foreach (ITestCaseData testCase in source.GetTestCasesFor(method)) { var parameterSet = testCase as TestCaseParameters; if (parameterSet == null) { parameterSet = new TestCaseParameters(testCase); } testCases.Add(BuildTestMethod(method, parentSuite, parameterSet)); } } if (method.GetParameters().Length == 0) { return(testCases); } ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method); methodSuite.ApplyAttributesToTest(method); foreach (TestMethod testCase in testCases) { methodSuite.Add(testCase); } return(new [] { methodSuite }); }
/// <summary> /// Validate general http request status by /// differnt parameters for Azure web service.. /// <param name="nodeName">different alphabet node name</param> /// </summary> void ValidateGeneralGetRequestStatusMethod(string nodeName) { // Gets the search query parameter and their values. string querySequence = _utilityObj._xmlUtil.GetTextValue( nodeName, Constants.QuerySequency); string queryDatabaseValue = _utilityObj._xmlUtil.GetTextValue( nodeName, Constants.DatabaseValue); string queryProgramValue = _utilityObj._xmlUtil.GetTextValue( nodeName, Constants.ProgramValue); string queryDatabaseParameter = _utilityObj._xmlUtil.GetTextValue( nodeName, Constants.DatabaseParameter); string queryProgramParameter = _utilityObj._xmlUtil.GetTextValue( nodeName, Constants.ProgramParameter); // Set Blast Parameters BlastParameters queryParams = new BlastParameters(); queryParams.Add(queryDatabaseParameter, queryDatabaseValue); queryParams.Add(queryProgramParameter, queryProgramValue); Dictionary <string, object> testCaseParms = new Dictionary <string, object>(); testCaseParms.Add(Constants.BlastParmsConst, queryParams); testCaseParms.Add(Constants.QuerySeqString, querySequence); TestCaseParameters parameters = new TestCaseParameters( Constants.AzureWebServiceRequestStatusForDnaTest, null, GetRequestStatus, testCaseParms); object resultsObject = _TestCaseSimulator.Simulate(parameters).Result; ServiceRequestInformation reqInfo = resultsObject as ServiceRequestInformation; // Validate job status. if (reqInfo.Status != ServiceRequestStatus.Waiting && reqInfo.Status != ServiceRequestStatus.Ready && reqInfo.Status != ServiceRequestStatus.Queued) { string error = ApplicationLog.WriteLine(string.Format( null, "Unexpected error", reqInfo.Status)); Assert.Fail(error); Console.WriteLine(string.Format((IFormatProvider)null, "Azure Blast BVT: Unexpected error", reqInfo.Status)); } else { Console.WriteLine(string.Format((IFormatProvider)null, "Azure Blast BVT: Client Request status has been validated successfully.")); Console.WriteLine(string.Format((IFormatProvider)null, "Azure Blast BVT: Request status {0} ", reqInfo.Status)); } }
public void RunMachineLearningTestCase() { double[] resultMLTestCase; if (testCaseOption == TestCaseOption.USE_IMAGES && (images == null || images.Count == 0)) { Debug.LogError("Images not loaded"); } if (totalWantedImagesToTest == 0) { imagesToTest = null; } TestCaseParameters param = MachineLearningTestCase.GetTestOption(testCaseOption, algoUsed); if (testCaseOption == TestCaseOption.USE_IMAGES) { param.neuronsPerLayer[0] = nbPixelInImage; param.neuronsPerLayer[param.neuronsPerLayer.Count - 1] = 1; param.X = images; param.Y = imageOutputs; param.sampleSize = nbImagesLoaded; param.gamma = (float)gamma; param.nbCentroids = nbCentroid; } if (algoUsed == AlgoUsed.RBF) { resultMLTestCase = RBFController.TrainRBFodel(this, param, imagesToTest, nbImagesToTestLoaded); } else { resultMLTestCase = MachineLearningTestCase.RunMachineLearningTestCase(algoUsed == AlgoUsed.LINEAR, param, epochs, learningRate, simulateTestCaseParameters, algoUsed, imagesToTest, nbImagesToTestLoaded); } if (testCaseOption != TestCaseOption.USE_IMAGES && testCaseOption != TestCaseOption.IMAGES_TEST) { DisplayOutput(resultMLTestCase); } else { foreach (var data in resultMLTestCase) { Debug.Log(data < 0.1 ? "Druid" : "Paladin"); } } simulateTestCaseParameters = null; }
IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite, IScenario scenario) { var builder = new NUnitTestCaseBuilder(); var resolvedParameters = method.GetParameters() .Select(p => scenario.DiContainer.TryResolve(p.ParameterType)) .ToArray(); var tcParams = new TestCaseParameters(resolvedParameters); var testMethod = builder.BuildTestMethod(method, suite, tcParams); testMethod.Properties.Add(ScenarioAdapter.ScreenplayScenarioKey, scenario); return(new[] { testMethod }); }
TestMethod ISimpleTestBuilder.BuildFrom(IMethodInfo method, Test suite) { TestCaseParameters parms = new TestCaseParameters(); parms.ExpectedResult = new object(); parms.HasExpectedResult = true; var t = _builder.BuildTestMethod(method, suite, parms); if (t.parms != null) { t.parms.HasExpectedResult = false; } return(t); }
private static TestCaseParameters GetParametersForMethod(IMethodInfo method, object[] args, int autoDataStartIndex) { var result = new TestCaseParameters(args); EnsureOriginalArgumentsArrayIsNotShared(result); var methodParameters = method.GetParameters(); for (int i = autoDataStartIndex; i < result.OriginalArguments.Length; i++) { result.OriginalArguments[i] = new TypeNameRenderer(methodParameters[i].ParameterType); } return(result); }
/// <summary> /// Builds the parameters /// </summary> protected virtual TestCaseParameters GetParametersForTestCase(IMethodInfo method) { TestCaseParameters parms; try { IParameterInfo[] parameters = method.GetParameters(); parms = GetParametersForTestCaseHelper(parameters, method); } catch (Exception ex) { parms = new TestCaseParameters(ex); } return(parms); }
/// <summary> /// Builds a single NUnitTestMethod, either as a child of the fixture /// or as one of a set of test cases under a ParameterizedTestMethodSuite. /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parentSuite">The suite or fixture to which the new test will be added</param> /// <param name="parms">The ParameterSet to be used, or null</param> /// <returns></returns> public TestMethod BuildTestMethod(IMethodInfo method, Test parentSuite, TestCaseParameters parms) { var testMethod = new TestMethod(method, parentSuite) { Seed = _randomizer.Next() }; CheckTestMethodSignature(testMethod, parms); if (parms == null || parms.Arguments == null) testMethod.ApplyAttributesToTest(method.MethodInfo); // NOTE: After the call to CheckTestMethodSignature, the Method // property of testMethod may no longer be the same as the // original MethodInfo, so we don't use it here. string prefix = testMethod.Method.TypeInfo.FullName; // Needed to give proper fullname to test in a parameterized fixture. // Without this, the arguments to the fixture are not included. if (parentSuite != null) prefix = parentSuite.FullName; if (parms != null) { parms.ApplyToTest(testMethod); if (parms.TestName != null) { // The test is simply for efficiency testMethod.Name = parms.TestName.Contains("{") ? new TestNameGenerator(parms.TestName).GetDisplayName(testMethod, parms.OriginalArguments) : parms.TestName; } else { testMethod.Name = _nameGenerator.GetDisplayName(testMethod, parms.OriginalArguments); } } else { testMethod.Name = _nameGenerator.GetDisplayName(testMethod, null); } testMethod.FullName = prefix + "." + testMethod.Name; return testMethod; }
/// <summary> /// Builds a single NUnitTestMethod, either as a child of the fixture /// or as one of a set of test cases under a ParameterizedTestMethodSuite. /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parentSuite">The suite or fixture to which the new test will be added</param> /// <param name="parms">The ParameterSet to be used, or null</param> /// <returns></returns> public TestMethod BuildTestMethod(IMethodInfo method, Test parentSuite, TestCaseParameters parms) { var testMethod = new TestMethod(method, parentSuite) { Seed = randomizer.Next() }; CheckTestMethodSignature(testMethod, parms); if (parms == null || parms.Arguments == null) { testMethod.ApplyAttributesToTest(method.MethodInfo); } if (parms != null) { // NOTE: After the call to CheckTestMethodSignature, the Method // property of testMethod may no longer be the same as the // original MethodInfo, so we reassign it here. method = testMethod.Method; string prefix = method.TypeInfo.FullName; // Needed to give proper fullname to test in a parameterized fixture. // Without this, the arguments to the fixture are not included. if (parentSuite != null) { prefix = parentSuite.FullName; } if (parms.TestName != null) { testMethod.Name = parms.TestName; testMethod.FullName = prefix + "." + parms.TestName; } else if (parms.OriginalArguments != null) { string name = method.GetDisplayName(parms.OriginalArguments); testMethod.Name = name; testMethod.FullName = prefix + "." + name; } parms.ApplyToTest(testMethod); } return(testMethod); }
public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite) { var builder = new NUnitTestCaseBuilder(); TestMethod test = null; var hasTest = false; foreach (var provider in _providerNames) { var isIgnore = !UserProviders.ContainsKey(provider); var data = new TestCaseParameters(new object[] { provider }); test = builder.BuildTestMethod(method, suite, data); test.Properties.Set(PropertyNames.Category, provider); SetName(test, method, provider, false); if (isIgnore) { test.RunState = RunState.Ignored; test.Properties.Set(PropertyNames.SkipReason, "Provider is disabled. See UserDataProviders.txt"); continue; } hasTest = true; yield return(test); if (_includeLinqService) { data = new TestCaseParameters(new object[] { provider + ".LinqService" }); test = builder.BuildTestMethod(method, suite, data); test.Properties.Set(PropertyNames.Category, provider); SetName(test, method, provider, true); yield return(test); } } if (!hasTest) { yield return(test); } }
/// <summary> /// Builds the parameters /// </summary> protected override TestCaseParameters GetParametersForTestCase(IMethodInfo method) { TestCaseParameters parms; try { var parameterlist = method.GetParameters().ToList(); parameterlist.RemoveAt(0); IParameterInfo[] parameters = parameterlist.ToArray(); parms = GetParametersForTestCaseHelper(parameters, method); } catch (Exception ex) { parms = new TestCaseParameters(ex); } return(parms); }
IEnumerable <TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite) { if (!method.IsGenericMethodDefinition) { return(base.BuildFrom(method, suite)); } if (TypeArguments == null || TypeArguments.Length != method.GetGenericArguments().Length) { var parms = new TestCaseParameters { RunState = RunState.NotRunnable }; parms.Properties.Set("_SKIPREASON", $"{nameof(TypeArguments)} should have {method.GetGenericArguments().Length} elements"); return(new[] { new NUnitTestCaseBuilder().BuildTestMethod(method, suite, parms) }); } var genMethod = method.MakeGenericMethod(TypeArguments); return(base.BuildFrom(genMethod, suite)); }
/// <summary> /// Builds a single NUnitTestMethod, either as a child of the fixture /// or as one of a set of test cases under a ParameterizedTestMethodSuite. /// </summary> /// <param name="method">The MethodInfo from which to construct the TestMethod</param> /// <param name="parentSuite">The suite or fixture to which the new test will be added</param> /// <param name="parms">The ParameterSet to be used, or null</param> /// <returns></returns> public TestMethod BuildTestMethod(MethodInfo method, Test parentSuite, TestCaseParameters parms) { var testMethod = new TestMethod(method, parentSuite) { Seed = randomizer.Next() }; string prefix = method.ReflectedType.FullName; // Needed to give proper fullname to test in a parameterized fixture. // Without this, the arguments to the fixture are not included. if (parentSuite != null) prefix = parentSuite.FullName; if (CheckTestMethodSignature(testMethod, parms)) { if (parms == null || parms.Arguments == null) testMethod.ApplyAttributesToTest(method); } if (parms != null) { // NOTE: After the call to CheckTestMethodSignature, the Method // property of testMethod may no longer be the same as the // original MethodInfo, so we reassign it here. method = testMethod.Method; if (parms.TestName != null) { testMethod.Name = parms.TestName; testMethod.FullName = prefix + "." + parms.TestName; } else if (parms.OriginalArguments != null) { string name = MethodHelper.GetDisplayName(method, parms.OriginalArguments); testMethod.Name = name; testMethod.FullName = prefix + "." + name; } parms.ApplyToTest(testMethod); } return testMethod; }
/// <summary> /// Gets the test cases generated by the CombiningStrategy. /// </summary> /// <returns>The test cases.</returns> public IEnumerable <ITestCaseData> GetTestCases(IEnumerable[] sources) { List <ITestCaseData> testCases = new List <ITestCaseData>(); IEnumerator[] enumerators = new IEnumerator[sources.Length]; int index = -1; for (; ;) { while (++index < sources.Length) { enumerators[index] = sources[index].GetEnumerator(); if (!enumerators[index].MoveNext()) { return(testCases); } } object[] testdata = new object[sources.Length]; for (int i = 0; i < sources.Length; i++) { testdata[i] = enumerators[i].Current; } TestCaseParameters parms = new TestCaseParameters(testdata); testCases.Add(parms); index = sources.Length; while (--index >= 0 && !enumerators[index].MoveNext()) { ; } if (index < 0) { break; } } return(testCases); }
IEnumerable <TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite) { // If the system doesn't support exceptions (Unity editor for delegates) we should not test with exceptions bool skipTest = (ExpectCompilerException || ExpectedException != null) && !SupportException; var expectResult = !method.ReturnType.IsType(typeof(void)); var name = method.Name; var arguments = this.Arguments; var permutations = CreatePermutation(0, arguments, method.GetParameters()); // TODO: Workaround for a scalability bug with R# or Rider // Run only one testcase if not running from the commandline if (!IsCommandLine()) { permutations = permutations.Take(1); } foreach (var newArguments in permutations) { var caseParameters = new TestCaseParameters(newArguments); if (expectResult) { caseParameters.ExpectedResult = true; if (OverrideResultOnMono != null && IsMono()) { caseParameters.Properties.Set(nameof(OverrideResultOnMono), OverrideResultOnMono); } } // Transfer FastMath parameter to the compiler caseParameters.Properties.Set(nameof(FastMath), FastMath); var test = _builder.BuildTestMethod(method, suite, caseParameters); if (skipTest) { test.RunState = RunState.Skipped; test.Properties.Add(PropertyNames.SkipReason, "Exceptions are not supported"); } yield return(test); } }
public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test?suite) { int count = 0; foreach (TestCaseParameters parms in GetTestCasesFor(method)) { count++; yield return(_builder.BuildTestMethod(method, suite, parms)); } // If count > 0, error messages will be shown for each case // but if it's 0, we need to add an extra "test" to show the message. if (count == 0 && method.GetParameters().Length == 0) { var parms = new TestCaseParameters(); parms.RunState = RunState.NotRunnable; parms.Properties.Set(PropertyNames.SkipReason, "TestCaseSourceAttribute may not be used on a method without parameters"); yield return(_builder.BuildTestMethod(method, suite, parms)); } }
/// <summary> /// Construct one or more TestMethods from a given MethodInfo, /// using available parameter data. /// </summary> /// <param name="method">The MethodInfo for which tests are to be constructed.</param> /// <param name="suite">The suite to which the tests will be added.</param> /// <returns>One or more TestMethods</returns> public IEnumerable<TestMethod> BuildFrom(IMethodInfo method, Test suite) { List<TestMethod> tests = new List<TestMethod>(); IParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 0) { IEnumerable[] sources = new IEnumerable[parameters.Length]; try { for (int i = 0; i < parameters.Length; i++) sources[i] = _dataProvider.GetDataFor(parameters[i]); } catch (InvalidDataSourceException ex) { var parms = new TestCaseParameters(); parms.RunState = RunState.NotRunnable; parms.Properties.Set(PropertyNames.SkipReason, ex.Message); tests.Add(_builder.BuildTestMethod(method, suite, parms)); return tests; } foreach (var parms in _strategy.GetTestCases(sources)) tests.Add(_builder.BuildTestMethod(method, suite, (TestCaseParameters)parms)); } return tests; }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the _values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> /// <remarks> /// The return value is no longer used internally, but is retained /// for testing purposes. /// </remarks> private static bool CheckTestMethodSignature(TestMethod testMethod, TestCaseParameters parms) { if (testMethod.Method.IsAbstract) return MarkAsNotRunnable(testMethod, "Method is abstract"); if (!testMethod.Method.IsPublic) return MarkAsNotRunnable(testMethod, "Method is not public"); IParameterInfo[] parameters; #if NETCF if (testMethod.Method.IsGenericMethodDefinition) { if (parms != null && parms.Arguments != null) { var mi = testMethod.Method.MakeGenericMethodEx(parms.Arguments); if (mi == null) return MarkAsNotRunnable(testMethod, "Cannot determine generic types by probing"); testMethod.Method = mi; parameters = testMethod.Method.GetParameters(); } else parameters = new IParameterInfo[0]; } else parameters = testMethod.Method.GetParameters(); int minArgsNeeded = parameters.Length; #else parameters = testMethod.Method.GetParameters(); int minArgsNeeded = 0; foreach (var parameter in parameters) { // IsOptional is supported since .NET 1.1 if (!parameter.IsOptional) minArgsNeeded++; } #endif int maxArgsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.parms = parms; testMethod.RunState = parms.RunState; arglist = parms.Arguments; if (arglist != null) argsProvided = arglist.Length; if (testMethod.RunState != RunState.Runnable) return false; } #if NETCF ITypeInfo returnType = testMethod.Method.IsGenericMethodDefinition && (parms == null || parms.Arguments == null) ? new TypeWrapper(typeof(void)) : testMethod.Method.ReturnType; #else ITypeInfo returnType = testMethod.Method.ReturnType; #endif #if NET_4_0 || NET_4_5 || PORTABLE if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method.MethodInfo)) { if (returnType.IsType(typeof(void))) return MarkAsNotRunnable(testMethod, "Async test method must have non-void return type"); var returnsGenericTask = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task<>); if (returnsGenericTask && (parms == null || !parms.HasExpectedResult)) return MarkAsNotRunnable(testMethod, "Async test method must have non-generic Task return type when no result is expected"); if (!returnsGenericTask && parms != null && parms.HasExpectedResult) return MarkAsNotRunnable(testMethod, "Async test method must have Task<T> return type when a result is expected"); } else #endif if (returnType.IsType(typeof(void))) { if (parms != null && parms.HasExpectedResult) return MarkAsNotRunnable(testMethod, "Method returning void cannot have an expected result"); } else if (parms == null || !parms.HasExpectedResult) return MarkAsNotRunnable(testMethod, "Method has non-void return value, but no result is expected"); if (argsProvided > 0 && maxArgsNeeded == 0) return MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any"); if (argsProvided == 0 && minArgsNeeded > 0) return MarkAsNotRunnable(testMethod, "No arguments were provided"); if (argsProvided < minArgsNeeded) return MarkAsNotRunnable(testMethod, string.Format("Not enough arguments provided, provide at least {0} arguments.", minArgsNeeded)); if (argsProvided > maxArgsNeeded) return MarkAsNotRunnable(testMethod, string.Format("Too many arguments provided, provide at most {0} arguments.", maxArgsNeeded)); if (testMethod.Method.IsGenericMethodDefinition && arglist != null) { var typeArguments = new GenericMethodHelper(testMethod.Method.MethodInfo).GetTypeArguments(arglist); foreach (Type o in typeArguments) if (o == null || o == TypeHelper.NonmatchingType) return MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method"); testMethod.Method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); } if (arglist != null && parameters != null) TypeHelper.ConvertArgumentList(arglist, parameters); return true; }
/// <summary> /// Gets the test cases generated by this strategy instance. /// </summary> /// <returns>A set of test cases.</returns> public IEnumerable<ITestCaseData> GetTestCases(IEnumerable[] sources) { List<ITestCaseData> testCases = new List<ITestCaseData>(); List<object>[] valueSet = CreateValueSet(sources); int[] dimensions = CreateDimensions(valueSet); IEnumerable pairwiseTestCases = new PairwiseTestCaseGenerator().GetTestCases( dimensions ); foreach (TestCaseInfo pairwiseTestCase in pairwiseTestCases) { object[] testData = new object[pairwiseTestCase.Features.Length]; for (int i = 0; i < pairwiseTestCase.Features.Length; i++) { testData[i] = valueSet[i][pairwiseTestCase.Features[i]]; } TestCaseParameters parms = new TestCaseParameters(testData); testCases.Add(parms); } return testCases; }
/// <summary> /// Construct one or more TestMethods from a given MethodInfo, /// using available parameter data. /// </summary> /// <param name="method">The MethodInfo for which tests are to be constructed.</param> /// <param name="suite">The suite to which the tests will be added.</param> /// <returns>One or more TestMethods</returns> public IEnumerable<TestMethod> BuildFrom(IMethodInfo method, Test suite) { List<TestMethod> tests = new List<TestMethod>(); #if NETCF if (method.ContainsGenericParameters) { var genericParams = method.GetGenericArguments(); var numGenericParams = genericParams.Length; var o = new object(); var tryArgs = Enumerable.Repeat(o, numGenericParams).ToArray(); IMethodInfo mi; try { // This fails if the generic method has constraints // that are not met by object. mi = method.MakeGenericMethodEx(tryArgs); if (mi == null) return tests; } catch { return tests; } var par = mi.GetParameters(); if (par.Length == 0) return tests; var sourceData = par.Select(p => _dataProvider.GetDataFor(p)).ToArray(); foreach (var parms in _strategy.GetTestCases(sourceData)) { mi = method.MakeGenericMethodEx(parms.Arguments); if (mi == null) { var tm = new TestMethod(method, suite); tm.RunState = RunState.NotRunnable; tm.Properties.Set(PropertyNames.SkipReason, "Incompatible arguments"); tests.Add(tm); } else tests.Add(_builder.BuildTestMethod(mi, suite, (TestCaseParameters)parms)); } return tests; } #endif IParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 0) { IEnumerable[] sources = new IEnumerable[parameters.Length]; try { for (int i = 0; i < parameters.Length; i++) sources[i] = _dataProvider.GetDataFor(parameters[i]); } catch (InvalidDataSourceException ex) { var parms = new TestCaseParameters(); parms.RunState = RunState.NotRunnable; parms.Properties.Set(PropertyNames.SkipReason, ex.Message); tests.Add(_builder.BuildTestMethod(method, suite, parms)); return tests; } foreach (var parms in _strategy.GetTestCases(sources)) tests.Add(_builder.BuildTestMethod(method, suite, (TestCaseParameters)parms)); } return tests; }