【问题标题】:My test case is not executing, it starts executing but nothing happens and it shows not run state我的测试用例没有执行,它开始执行但什么也没发生,它显示未运行状态
【发布时间】:2020-03-03 10:20:27
【问题描述】:

我的测试用例没有执行,它开始执行但没有任何反应,并且显示未运行状态。 当我点击运行测试时,excel 文件没有打开并且测试运行显示Not Run。我没有得到什么问题,因为没有显示错误。请指导我。

【问题讨论】:

  • 用 [TestMethod] 属性装饰你的 button1_click。
  • 测试方法必须满足给定的要求:该方法必须在方法名称上方使用 [TestMethod] 属性定义。该方法必须具有返回类型 void。该方法不能有任何参数。
  • @Atk 如何使用 [TestMethod] 属性装饰您的 button1_click?

标签: c# excel unit-testing visual-studio-2019


【解决方案1】:

应该是这样的:

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);
}
}
}

我补充说:

private bool show_messages = true; 

然后创建新的测试方法:

[TestMethod] 
public void TestExcel_RunExceldownload_OK() 

一些小改动

private bool Exceldownload(List<string> lst) 

在测试方法时不显示消息框。 我将 void 更改为 bool,以进行 Assert。

一般来说,如果你想让测试工作,你必须编写一个公共的测试方法并添加适当的属性:[TestMethod]。 此外,要编写好的测试尝试使用 Arrange-Act-Assert 模式, 请参阅例如:https://github.com/testdouble/contributing-tests/wiki/Arrange-Act-Assert

【讨论】:

  • 请概述您更改了哪些行,以便更改更明显。
  • 嗨,我补充说:private bool show_messages = true;然后创建了新的测试方法: [TestMethod] public void TestExcel_RunExceldownload_OK() 在私有 bool Exceldownload(List lst) 中进行了一些小的更改,以不在测试方法中显示消息框。我将 void 更改为 bool,以创建 Assert。
  • 如果您将评论详细信息移动到答案中,那将是完美的;)
  • @MarcinPol 代码未显示与列表值不匹配的列值。
  • 您可以将消息写入输出。请看:stackoverflow.com/questions/4786884/…
【解决方案2】:

这里有几件事。首先检查unit testing TestMethod requirements in the official documentation

测试方法必须满足以下要求:

  • 列表项

  • 用 [TestMethod] 属性修饰。

  • 它返回 void。

它不能有参数。

这意味着您应该按照@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

现在您的测试执行了,i 远高于原始长度 3。

尝试改变

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

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 2017-02-08
    相关资源
    最近更新 更多