How to pass NUnit test when reading the data from excel file?

MYJZ003

New member
Joined
Jun 12, 2023
Messages
3
Programming Experience
Beginner
At ExcelReader.cs, I got the code:
1686544308704.png

1686544329548.png


At ExcelReader_Tests.cs, I have the code:
1686544351268.png


I have the excel file, "Registration_Data.xlsx" and I put in the following:
Name | Email | Phone | Password
Mark | Testing | 12345678 | M456912

After I run, the result is exited with code 0.

How will I solve this problem to pass NUnit test when reading the data from excel file?
 
Please don't post pictures of code. We can't copy text from a picture if we want to run code or search for error messages. ALWAYS post code as text, formatted as code. Post a picture as well if it adds value (it doesn't in this case) but never instead of text.
 
Okay. Sorry.

At ExcelReader.cs, I got the code:
C#:
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace ExcelRead
{
    public class ExcelReader
    {
        public static IEnumerable<TestCaseData> ReadFromExcel(string excelFileName, string excelsheetTabName)
        {
            string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string xslLocation = Path.Combine(executableLocation, "data/" + excelFileName);

            string cmdText = "SELECT * FROM [" + excelsheetTabName + "$]";

            if (!File.Exists(xslLocation))
                throw new Exception(string.Format("File name: {0}", xslLocation), new FileNotFoundException());

            string connectionStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;" +
                "HDR=YES\";", xslLocation);

            var testCases = new List<TestCaseData>();
            using (var connection = new OleDbConnection(connectionStr))
            {
                connection.Open();
                var command = new OleDbCommand(cmdText, connection);
                var reader = command.ExecuteReader();
                if (reader == null)
                    throw new Exception(string.Format("No data return from file, file name:{0}", xslLocation));
                while (reader.Read())
                {
                    var row = new List<string>();
                    var feildCnt = reader.FieldCount;
                    for (var i = 0; i < feildCnt; i++)
                        row.Add(reader.GetValue(i).ToString());
                    testCases.Add(new TestCaseData(row.ToArray()));
                }
            }
            if (testCases != null)
                foreach (TestCaseData testCaseData in testCases)
                    yield return testCaseData;
        }
    }
}


At ExcelReader_Tests, I have the code:
C#:
using NUnit.Framework;
using System.Drawing.Printing;
using System.Reflection;

namespace ExcelRead.Tests
{
    [TestFixture]
    public class ExcelReader_Tests
    {
        private static string FILENAME = "Registration_Data.xlsx";
        public static IEnumerable<TestCaseData> RegistrationData()
        {
            return ExcelReader.ReadFromExcel(FILENAME, "Registration");
        }
        [TestCaseSource(nameof(RegistrationData))]
        public void Registration_Data(string Name, string Email, string Phone, string Password)
        {
            Assert.Multiple(() =>
            {
                Assert.IsNotEmpty(Name);
                Assert.IsNotEmpty(Email);
                Assert.IsNotEmpty(Phone);
                Assert.IsNotEmpty(Password);
            });
        }
    }
}

I have the excel file, "Registration_Data.xlsx" and I put in the following:
Name | Email | Phone | Password
Mark | Testing | 12345678 | M456912

After I run, the result is exited with code 0.

How will I solve this problem to pass NUnit test when reading the data from excel file?
 
Last edited by a moderator:
I specifically said:
post code as text, formatted as code
but you made no attempt to format the code snippets. I have done it for you this time. Please do it yourself in future.
 
It is a unit test.

Then you should absolutely not be reading data from an external file. You might use canned data in a unit test but reading files would be done as part of integration testing.
 
Back
Top Bottom