My test case is not executing, it starts executing but nothing happens and it shows not run state.
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.

- 55
- 9
-
Decorate your button1_click with [TestMethod] attribute. – Atk Mar 03 '20 at 10:32
-
A test method must meet the given requirements: The method must be defined with the [TestMethod] attribute just above method name. The method must having return type void. The method cannot have any parameters. – Atk Mar 03 '20 at 10:33
-
@Atk how can I Decorate your button1_click with [TestMethod] attribute? – Mehwish Mar 03 '20 at 11:07
2 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

- 21
- 3
-
Please outline which lines you changed so the changes are more obviuos. – Markus Deibel Mar 03 '20 at 10:51
-
Hi, 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
lst) to not show messages boxes in the test method. I changed void to bool, to make Assert. – Marcin Pol Mar 03 '20 at 12:03 -
If you move the comment details into the answer, it would be perfect ;) – Markus Deibel Mar 03 '20 at 12:07
-
@MarcinPol Code is not showing which column value miss-matches the list value. – Mehwish Mar 03 '20 at 12:20
-
You can write the message to output. Please see: https://stackoverflow.com/questions/4786884/how-to-write-output-from-a-unit-test – Marcin Pol Mar 03 '20 at 12:49
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

- 25,191
- 4
- 32
- 61