Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的测试用例没有执行,它开始执行,但什么都没有发生,并且它显示为not run状态_C#_Excel_Unit Testing_Visual Studio 2019 - Fatal编程技术网

C# 我的测试用例没有执行,它开始执行,但什么都没有发生,并且它显示为not run状态

C# 我的测试用例没有执行,它开始执行,但什么都没有发生,并且它显示为not run状态,c#,excel,unit-testing,visual-studio-2019,C#,Excel,Unit Testing,Visual Studio 2019,我的测试用例没有执行,它开始执行,但什么也没有发生,并且它显示not run状态。 当我单击RunTest时,excel文件不会打开,TestRun显示not run。我不明白问题是什么,因为没有显示错误。请引导我。应该是这样的: using Microsoft.VisualStudio.TestTools.UnitTesting; using Excel = Microsoft.Office.Interop.Excel; using Xceed.Wpf.Toolkit; using Syste

我的测试用例没有执行,它开始执行,但什么也没有发生,并且它显示not run状态。
当我单击RunTest时,excel文件不会打开,TestRun显示
not run
。我不明白问题是什么,因为没有显示错误。请引导我。

应该是这样的:

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);
}
}
}
然后创建了新的测试方法:

[TestMethod] 
public void TestExcel_RunExceldownload_OK() 
一些小的变化

private bool Exceldownload(List<string> lst) 
private bool excel下载(列表lst)
测试方法时不显示消息框。 我把void改成bool,做Assert

通常,如果希望测试正常工作,则必须编写公共测试方法并添加适当的属性:[TestMethod]。 此外,要编写好的测试,请尝试使用Arrange-Act-Assert模式,
请参见示例:

这里有几件事。首先检查

试验方法必须满足以下要求:
  • 列表项

  • 它由[TestMethod]属性修饰

  • 它返回无效

它不能有参数

这意味着您应该更改@Marcin提到的代码

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

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

         public void Exceldownload()
        {
            // Your code.
        }
我能看到的第二件事是,你的代码中断了,这就是为什么你把参数弄得超出了范围。你的问题是:

  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

用[TestMethod]属性装饰按钮1\u单击。测试方法必须满足给定的要求:该方法必须使用方法名称正上方的[TestMethod]属性定义。该方法必须具有返回类型void。该方法不能有任何参数。@Atk如何用[TestMethod]属性装饰您的按钮1\u单击?请概述您更改的行,以便更改更明显。您好,我添加了:private bool show\u messages=true;然后创建了新的测试方法:[TestMethod]public void TestExcel_RunExceldownload_OK(),对私有bool Exceldownload(列表lst)进行了一些小的更改,以在测试方法中不显示消息框。我将void更改为bool,以进行断言。如果您将注释详细信息移动到答案中,这将是完美的;)@MarcinPol代码未显示与列表值匹配的列值。您可以将消息写入输出。请参阅:
  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
if (lst.Count > i-2 && !lst[i - 1].Equals(str)) // Problematic row