C#方法未打印正确的值

问题描述

| 我下面有两种方法General和SearchTab。当查看报告文件时,我看到常规方法的值如下: 测试名称: TestMachine:Maya TestUser:管理员 测试时间:6/13/2011 12:02 TestStatus:失败 TestExpectedResult: TestActualResult: 测试评论: TestName,TestExpectedResult,TestActualResult和TestComments为空白,但它们应具有以下值: TestName:测试1:常规, TestExpectedResult:应该显示\'Home \'标签, TestActualResult:找到“主页”选项卡 TestComments:找到“主页”选项卡 另外,TestStatus应该是PASS而不是FAIL。 看起来,即使我在General方法中重新分配了这些变量的值,它们仍在打印在构造函数中分配给它们的值。 问题与第二种方法SearchTab相同。 请帮助我找出问题所在。
namespace Automation
{
[TestClass]
public class FunctionalTest
{
    public ISelenium Sel;
    public StringBuilder Err;
    public string Report = \"C:\\Report.XLS\";
    public string[] arrTestResults = new string[8];
    public string TestName;
    public string TestMachine;
    public string TestUser;
    public string TestTime;
    public string TestStatus;
    public string TestExpectedResult;
    public string TestActualResult;
    public string TestComments;

    // Constructor
    public FunctionalTest()
    {

        TestName = arrTestResults[0];
        TestMachine = arrTestResults[1] = System.Environment.MachineName.ToString();
        TestUser = arrTestResults[2] = System.Environment.UserName.ToString();
        TestTime = arrTestResults[3] = System.DateTime.Now.ToString();
        TestStatus = arrTestResults[4] = \"FAIL\";
        TestExpectedResult = arrTestResults[5];
        TestActualResult = arrTestResults[6];
        TestComments = arrTestResults[7];
    }

    public void WriteReport(string[] arrResults)
    {
        int iLastRow,iCnt1;

        if (File.Exists(Report))
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            Object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkBook = xlApp.Workbooks.Open(Report,false,5,\"\",true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,\"\\t\",1,0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            iLastRow = range.Rows.Count;
            for (iCnt1 = 1; iCnt1 < 9; iCnt1++)
            {
                xlWorkSheet.Cells[iLastRow + 1,iCnt1] = arrResults[iCnt1 - 1];
            }
            xlWorkBook.Save();
            xlApp.Quit();
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }
        else
        {
            Console.WriteLine(\"Report File \" + Report + \" does not exist\");
        }
    }

    [TestMethod]
    public void General()
    {
        TestName = \"Test 1: General\";
        TestExpectedResult = \"\'Home\' tab should be present\";
        if (Sel.IsElementPresent(\"TAB_Home\")))
        {

                TestStatus = \"PASS\";
                TestActualResult = \"Home tab found\";
                TestComments = TestActualResult;
        }
            else
            {
                TestActualResult = \"Home tab not found\";
                TestComments = TestActualResult;

            }

            //Write to report
            WriteReport(arrTestResults);
        }
    }

    [TestMethod]
    public void SearchTab()
    {
        TestName = \"Test 2: Search Tab\";
        TestExpectedResult = \"Search tab should be present\";
        // Assigning TestStatus to FAIL because TestStatus is PASS right now from the previous test method
        TestStatus = \"FAIL\";

        if (Sel.IsElementPresent(\"TAB_Search\"))
        {
            sActual = \"Search Tab found\";
            arrTestResults[7] = sActual;
            TestComments = TestActualResult;
            TestStatus = \"PASS\";
        }
        else
        {
            TestActualResult = \"Search tab not found\";
            TestComments = TestActualResult;
        }

        //Write to report
        WriteReport(arrTestResults);
    }


}
}
    

解决方法

        初始化将创建初始数组值,并且永远不会更改它们。您正在更改字段值:
string TestName = \"test\";

TestName = arrTestResults[0];

// At this point,changing TestName does not affect arrTestResults[0]
另外,您不应使用公共字段。相反,您应该使用属性。
public string TestName { get; set; }  // auto-property
要么
private string _testName = string.Empty;
public string TestName { get { return _testName; } set { _testName = value; }}
您或者需要为更改数组的属性编写设置器,或者最好在报告中使用适当的字段/属性:
xlWorkSheet.Cells[iLastRow + 1,0] = TestName;
当然,这也可以重构为更好的方法。     ,        不要在构造函数或常规方法中分配值。根据您的代码,General似乎是一个测试用例。如果是测试用例,那么不要期望应该首先调用General,因为您将其定义为第一个方法。 使用SetupTest()设置值。请注意,SetupTest()不是测试用例,但是在执行每个测试用例之前都会调用它。 不打印,发出警报。
[SetUp]
public void SetupTest()
{
  ...your initialization code...
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...