Esempio n. 1
0
        public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite)
        {
            IParameterInfo[] methodParams = method.GetParameters();
            if (methodParams.Length != 2)
            {
                throw new TargetParameterCountException("JSON data source only supports 1 test methods accepting 1 parameter.");
            }


            var    tests   = new List <TestMethod>();
            string dirPath = GetDirPath(method);

            string[] jsonFiles = Directory.GetFiles(GetDirPath(method), "*.json", SearchOption.AllDirectories);


            foreach (var jsonFile in jsonFiles)
            {
                object[] args = new object[2];
                args[0] = Path.GetFileName(jsonFile).Replace(".json", "").Replace("_", " ");

                string strJson = File.ReadAllText(jsonFile);
                try
                {
                    Type paramType = methodParams[1].ParameterType;
                    var  obj       = JsonConvert.DeserializeObject(strJson, paramType);
                    args[1] = obj;

                    tests.Add(TestCaseBuilder.BuildTestMethod(method, suite, new TestCaseParameters(args)));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Caught Exception: " + ex.ToString());
                }
            }

            return(tests);
        }
Esempio n. 2
0
        public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite)
        {
            List <TestMethod>         tests        = new List <TestMethod>();
            List <TestCaseParameters> parameterSet = new List <TestCaseParameters>();

            ParameterInfo[] methodParams = method.MethodInfo.GetParameters();

            string filename = GetFilePath(method, "csv");

            if (File.Exists(filename))
            {
                var csv = new CsvHelper.CsvReader(File.OpenText(filename));
                csv.Configuration.Comment          = '#';
                csv.Configuration.IgnoreBlankLines = true;
                csv.Configuration.HasHeaderRecord  = true;
                csv.Configuration.AllowComments    = true;

                while (csv.Read())
                {
                    if (methodParams.Length != csv.CurrentRecord.Length)
                    {
                        throw new TargetParameterCountException("CSV data and method parameters count do not match.");
                    }

                    object[] args = new object[csv.CurrentRecord.Length];
                    for (int i = 0; i < csv.CurrentRecord.Length; i++)
                    {
                        string strValue = csv.CurrentRecord[i].Trim();

                        ParameterInfo paramInfo = methodParams[i];

                        var converter = TypeDescriptor.GetConverter(paramInfo.ParameterType);
                        if (converter != null)
                        {
                            try
                            {
                                var value = converter.ConvertFrom(strValue);
                                args[i] = value;
                            }
                            catch (System.FormatException)
                            {
                                if ("Boolean".Equals(paramInfo.ParameterType))
                                {
                                    // booleans are false by default
                                    args[i] = false;
                                }
                            }
                            catch (Exception e)
                            {
                                throw new TargetException("Unable to convert CSV data to target data type: " + strValue, e);
                            }
                        }
                        else
                        {
                            throw new TargetException("Unable to locate converter for CSV value: " + strValue);
                        }
                    }

                    tests.Add(TestCaseBuilder.BuildTestMethod(method, suite, new TestCaseParameters(args)));
                }
            }
            else
            {
                tests.Add(TestCaseBuilder.BuildTestMethod(method, suite, new TestCaseParameters()));
            }


            return(tests);
        }