1

In my project I have Test Data folder and inside that I have my xlsx file.

enter image description here

I am trying to get the project path folder so it read from their instead of hard coding the path. But it always goes and reads form project/bin/debug path. Here is my Excel utility. In my class I am Initializing the ExcelUtil class and using the ReadData method

    public class ExcelUtil
    {
        public static void InitializeExcel()
        {
            string exeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            File.OpenRead(System.IO.Path.Combine(exeDir, "Data.xlsx"));


        }

        public static DataTable ExcelToDataTable(string fileName)
        {
            //Open the file
            using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
            {
                //read the excel file
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        //using anaysome method
                        ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                        {
                            UseHeaderRow = true
                        }
                    });

                    //Storing in DataCollection
                    DataTableCollection table = result.Tables;
                    DataTable resultTable = table["Sheet1"];
                    return resultTable;
                }
            }
        }

        //storing the Data from excel in List type of othe custom class
        public static List<DataCollection> datacol = new List<DataCollection>();

        //Method populates the data into the collection
        public static void PopulateInCollection(string fileName)
        {
            DataTable table = ExcelToDataTable(fileName);

            //Iterating through the rows and columns
            for (int row = 1; row <= table.Rows.Count; row++)
            {
                for (int col = 0; col < table.Columns.Count; col++)
                {
                    DataCollection dTable = new DataCollection()
                    {
                        RowNumber = row,
                        ColName = table.Columns[col].ColumnName,
                        ColValue = table.Rows[row - 1][col].ToString()
                    };
                    //Add all the details for each row
                    datacol.Add(dTable);
                }
            }
        }

        //Method read data from excel using rownum and colname
        public static string ReadData(int rowNumber, string columnName)
        {
            try
            {
                //Retriving data using LINQO to reduce much iterations
                string data = (from colData in datacol
                               where colData.ColName == columnName && colData.RowNumber == rowNumber
                               select colData.ColValue).SingleOrDefault();

                return data.ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
    }

    //Custom class to hold rowsnum and columnnum adn val data
    public class DataCollection
    {
        public int RowNumber { get; set; }
        public string ColName { get; set; }
        public string ColValue { get; set; }
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Johnny_347
  • 53
  • 1
  • 7
  • You need to say which test framework you are using. NUnit for example has `TestContext.CurrentContext.TestDirectory` as it doesn't set the current directory when running tests. – Neil Mar 09 '20 at 17:02
  • Reading your question title and description, I thought you were doing unit or integration testing using already existing testing frameworks. – Cleptus Mar 09 '20 at 17:05
  • @Neil I am using the Nunit Framework. What should I chance in the InitializeExcel method – Johnny_347 Mar 09 '20 at 17:10

1 Answers1

1

As you are using NUnit you need to get the path from TestContext.CurrentContext.TestDirectory, which will be the folder the tests are executed from. Then just add your relative path to that:

public static void InitializeExcel()
{
    string testDataPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData");
    File.OpenRead(System.IO.Path.Combine(testDataPath, "Data.xlsx"));
}
Neil
  • 11,059
  • 3
  • 31
  • 56
  • With NUnit 3.x this points to `/bin/Debug/TestData`. How do you get it so either the files are copied to the Release/Debug folder or point to the relative directory? – Emmanuel F Feb 18 '21 at 17:18
  • @EmmanuelF In SolutionExplorer, find the file you want to copy to the output folder, and change "Copy To Output Folder" to "Copy if newer". The file will appear below the debug folder, relative to where is it in the project. – Neil Feb 19 '21 at 09:06
  • Thank you @Neil! I was able to figure out after fiddling with the settings. – Emmanuel F Feb 21 '21 at 05:27