Home » excel » c# – My test case is not executing, it starts executing but nothing happens and it shows not run state

c# – My test case is not executing, it starts executing but nothing happens and it shows not run state

Posted by: admin May 14, 2020 Leave a comment

Questions:

My test case is not executing, it starts executing but nothing happens and it shows not run state.What I have done is:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Excel = Microsoft.Office.Interop.Excel;
using Xceed.Wpf.Toolkit;
using System;
using System.Collections.Generic;

namespace Test01.AppObj
{
    [TestClass]
    public class MainProjectFile
    {

        bool control = true;

        [TestInitialize]
        //[Ignore]
        [Priority(1)]
        [TestMethod]

        public void Exceldownload(List<string> lst)
        {
            string str;
            int rw = 0;
            int cl = 0;


            Excel.Application xlApp = new Excel.Application();
            {
                xlApp.Visible = true;
                xlApp.DisplayAlerts = false;
            }

            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"path", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "", false, false, 0, true, false, false);
            xlApp.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized;

            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];

            Excel.Range xlRange = xlWorksheet.UsedRange;
            rw = xlRange.Rows.Count;
            cl = xlRange.Columns.Count;

            for (int i = 1; i <= cl; i++)
            {
                str = Convert.ToString((xlRange.Cells[2, i] as Excel.Range).Value2);
                if (!lst[i - 1].Equals(str))
                {
                    MessageBox.Show($"Not match in column: {i}");
                    control = false;
                }
            }
            if (control)
            {
                MessageBox.Show($"All match");
            }

            xlWorkbook.Close(0);
            xlApp.Quit();
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            List<string> lst1 = new List<string>() { "Abc", "10t", "88990" };
            Exceldownload(lst1);
        }
    }
}

But as I click on run test, the excel file does not open and test run shows Not Run. I am not getting what is the issue as no error is displayed. Please guide me.

How to&Answers:

It should be like this:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Excel = Microsoft.Office.Interop.Excel;
using Xceed.Wpf.Toolkit;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Test01.AppObj
{

[TestClass]
public class MainProjectFile
{

bool control = true;
private bool show_messages = true;

[TestMethod]
public void TestExcel_RunExceldownload_OK()
{
  //Arrange
  List<string> lst1 = new List<string>() { "Abc", "10t", "88990" };
  show_messages = false;

  //Act
  bool returnValue = Exceldownload(lst1);

  //Assert
  Debug.Assert(returnValue==true);
}


private bool Exceldownload(List<string> lst)
{
  string str;
  int rw = 0;
  int cl = 0;
  bool returnValue = false;

  Excel.Application xlApp = new Excel.Application();
  {
    xlApp.Visible = true;
    xlApp.DisplayAlerts = false;
  }

  Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"path", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "", false, false, 0, true, false, false);
  xlApp.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized;

  Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];

  Excel.Range xlRange = xlWorksheet.UsedRange;
  rw = xlRange.Rows.Count;
  cl = xlRange.Columns.Count;

  for (int i = 1; i <= cl; i++)
  {
    str = Convert.ToString((xlRange.Cells[2, i] as Excel.Range).Value2);
    if (!lst[i - 1].Equals(str))
    { 
      if (show_messages)
        MessageBox.Show($"Not match in column: {i}");
      control = false;
      returnValue = false;
    }
  }
  if (control)
  {
    returnValue = true;
    if (show_messages)
      MessageBox.Show($"All match");
  }

  xlWorkbook.Close(0);
  xlApp.Quit();
  return returnValue;
}

private void Button1_Click(object sender, EventArgs e)
{
  List<string> lst1 = new List<string>() { "Abc", "10t", "88990" };
  Exceldownload(lst1);
}
}
}

I added:

private bool show_messages = true; 

then created new test method:

[TestMethod] 
public void TestExcel_RunExceldownload_OK() 

some minor changes in

private bool Exceldownload(List<string> lst) 

to not show messages boxes when the method is tested.
I changed void to bool, to make Assert.

In general, if you want tests to work, you must write a public test method and add the appropriate attribute: [TestMethod].
In addition, to write good tests try to use Arrange-Act-Assert pattern,
please see e.g.: https://github.com/testdouble/contributing-tests/wiki/Arrange-Act-Assert

Answer:

There are a couple of things here. First of all check unit testing TestMethod requirements in the official documentation

A test method must meet the following requirements:

  • List item

  • It’s decorated with the [TestMethod] attribute.

  • It returns void.

It cannot have parameters.

This means that you should change the code as @Marcin mentions.

        [TestInitialize]
        //[Ignore]
        [Priority(1)]
        [TestMethod]

        public void ExceldownloadTest()
        {
            // Init list 
            this.Exceldownload(lst);
        }

         public void Exceldownload()
        {
            // Your code.
        }

Second thing I can see, is that your code breaks, and that’s why you get the argument out of range. Your problem is here:

  for (int i = 1; i <= cl; i++)
  {
    str = Convert.ToString((xlRange.Cells[2, i] as Excel.Range).Value2);
    if (!lst[i - 1].Equals(str)) // Problematic row

Now that your test executes, the i is much higher than 3, which was the original length.

Try to change with

if (lst.Count > i-2 && !lst[i - 1].Equals(str)) // Problematic row