jeudi 3 mars 2016

Parse params method via external data with ClassAttribute

I'm trying to create a class attribute that takes data from a spreadsheet and pass to the method parameters.

(already get data from a Excel file, but dont know how i can finish to initialize for example a test like this:

    [Test, MyCaseAttribute(@"my path here", @"my sheet here")]
    public void Test_Some_Thing(string A, string B, string C, string D, string result)

{ }

Currently the attribute class looks like this:

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class MyCaseAttribute: Attribute
{
    private string plan;
    private string sheet;

    public MyCaseAttribute(String plan, String sheet)
    {
        this.plan= plan;
        this.sheet= sheet;
    }

    public List<TestCaseData> ReadExcelData()
    {
        string connectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source=" + this.plan+ ";Extended Properties=Excel 8.0;";
        List<TestCaseData> testDataList = new List<TestCaseData>();
        using (OleDbConnection connection = new OleDbConnection(connectionStr))
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand("SELECT * FROM [" + this.sheet + "$]", connection);
            OleDbDataReader reader = command.ExecuteReader();

            int columCount= reader.FieldCount;
            columCount= columCount- 1;

            while (reader.Read())
            {
                string[] args = new string[columCount];

                for (int i = 0; i < columCount; i++)
                {
                    if (i == columCount)
                        args[i] = reader.GetValue(columCount).ToString();
                    else
                    {
                        args[i] = reader.GetValue(i + 1).ToString();
                    }
                }

                TestCaseData testData = new TestCaseData(args);
                testData.SetName(reader.GetString(0));
                testDataList.Add(testData);
            }
        }
        return testDataList;
    }

Aucun commentaire:

Enregistrer un commentaire