예제 #1
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/

        private static TestResult CheckTest(MethodBase method, UT.TestData data, int index)
        {
            if (data == null)
            {
                return new TestResult {
                           Status = oM.Test.TestStatus.Error, Description = "TestData", Message = "The provided TestData was null and could not be evaluated."
                }
            }
            ;

            string     description = "TestData: " + (!string.IsNullOrWhiteSpace(data.Name) ? $"name: {data.Name}," : "") + $"index: {index}";
            TestResult testResult  = new TestResult {
                Description = description
            };
            var result = Run(method, data);

            //Check if critical errors where raised while running the unit test
            if (result.Item2.Count != 0)
            {
                testResult.Status = oM.Test.TestStatus.Error;

                testResult.Message = "Failed to run unit test. Errors given: ";
                foreach (string error in result.Item2)
                {
                    testResult.Message += Environment.NewLine + error;
                }
                return(testResult);
            }

            try
            {
                if (result.Item1.Count != data.Outputs.Count)
                {
                    testResult.Status  = oM.Test.TestStatus.Error;
                    testResult.Message = "Execution of the method returned a different number of results compared to the expected output.";
                    return(testResult);
                }

                List <ComparisonDifference> differences = CompareResults(data.Outputs, result.Item1);

                if (differences.Count == 0)
                {
                    testResult.Status  = oM.Test.TestStatus.Pass;
                    testResult.Message = "Execution of the method returned the expected outputs.";
                }
                else
                {
                    testResult.Status      = oM.Test.TestStatus.Error;
                    testResult.Message     = "Execution of the method did not return the expected outputs.";
                    testResult.Information = differences.Cast <BH.oM.Test.ITestInformation>().ToList();
                }

                return(testResult);
            }
            catch (Exception e)
            {
                testResult.Status   = oM.Test.TestStatus.Error;
                testResult.Message  = "Failed to run the comparison of the result of execution of the method with the expected outputs." + Environment.NewLine;
                testResult.Message += $"Exception thrown: {e.Message}";
                return(testResult);
            }
        }
예제 #2
0
        public static Output <List <object>, List <string> > Run(MethodBase method, UT.TestData data)
        {
            if (method == null)
            {
                BH.Engine.Base.Compute.RecordError("Method to run a test on cannot be null.");
                return(null);
            }

            if (data == null)
            {
                BH.Engine.Base.Compute.RecordError("Data to use to test method with cannot be null.");
                return(null);
            }

            List <object> result = new List <object>();
            List <string> errors = new List <string>();

            if (method.IsGenericMethod && method is MethodInfo)
            {
                method = Engine.Base.Compute.MakeGenericFromInputs(method as MethodInfo, data.Inputs.Select(x => FixType(x)?.GetType()).ToList());
            }

            try
            {
                ParameterInfo[] parameters = method.GetParameters();
                if (data.Inputs.Count == parameters.Length)
                {
                    List <object> inputs = new List <object>();
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        inputs.Add(CastToType(data.Inputs[i], parameters[i].ParameterType));
                    }

                    object resultObject = method.Invoke(null, inputs.ToArray());

                    IOutput output = resultObject as IOutput;

                    if (output == null)
                    {
                        result.Add(resultObject);
                    }
                    else
                    {
                        for (int i = 0; i < output.OutputCount(); i++)
                        {
                            result.Add(output.IItem(i));
                        }
                    }
                }
                else
                {
                    errors.Add("The number of inputs is not matching the number of parameters");
                }
            }
            catch (Exception e)
            {
                string message = "Failed to run with the given inputs";
                if (e is NotImplementedException || e.InnerException is NotImplementedException)
                {
                    message = "The method is not implemented.";
                }
                else if (e is RuntimeBinderException || e.InnerException is RuntimeBinderException)
                {
                    message = "The method with the correct input types cannot be found.";
                }
                errors.Add(message);
            }

            return(new Output <List <object>, List <string> > {
                Item1 = result, Item2 = errors
            });
        }