如何从派生类的静态[TestCleanup]方法调用基类的非静态方法

时间:2013-01-30 12:57:35

标签: c# .net unit-testing testing automated-tests

背景:

我正在使用Selenium Web驱动程序底层的Visual Studio 2010测试项目进行UI测试。为此,

我定义了名为 TestCases 的[TestClass],它来自 base TestUtil 类,其中包含一些接受测试配置数据的通用实用程序。

问题:

成功完成所有测试用例执行后,我需要生成测试报告。 因此,我决定将所有与报告活动相关的代码放在我的 [ClassCleanup] 方法中。

实际问题是,报告代码的一部分实际上取决于来自 base TestUtil 类的Config参数。但是,因为我的 [ClassCleanup] 方法是静态,我无法访问 [ClassCleanup] 报告活动代码中的所有配置参数

我的 TestClass 看起来像:

   [TestClass]
    public class TestCases : TestCaseUtil
    {

        Browser browser;
        int stepNo;

        private static string configFilesLocation = String.Empty;
        //private static string 

        /// <summary>
        /// Defines test context
        /// </summary>
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        /// TestCases Class Constructor 
        /// </summary>
        public TestCases()
        {
            // Loads config data and creates a Singleton object of Configuration and loads data into generic test case variables
            GetConfigData();

            /// Get debug viewer exe file path
            configFilesLocation = PrepareConfigureDataFilePath();

            /// Prepare log directory details from xml file
            PrepareLogDirectoryPath(configFilesLocation);

            /// Initialize browser instance
            browser = null;
            applicationLog = null;
            stepNo = 1;
        }


        /// <summary>
        /// Test case to verify if create module and corresponding event works fine.
        /// </summary>
        [Ignore]
        [TestMethod, Description("Test case to verify if create module and corresponding event works fine.")]
        public void CreateAndVerifyEvent()
        {
            ///Test in Progress
        }

        /// <summary>
        **/// Test clean up activities**
        /// </summary>
        [TestCleanup]
        public void Cleanup()
        {
            **/// Test Clean up activities here**

        }

        /// <summary>
        **///  Test Class cleanup activities here**
        /// </summary>
        [ClassCleanup]
        public static void TestClassCleanupClass()
        {
           **/// This is **Reporting Code****

            try
            { 
                   **/// This code contains methods call and variable call from Base class (TestUtils), Which are obviously not working since Base class is not static class.**

                string ExecutionStartDateTime = GetValuesFromXML("TestDataConfig", "ExecutionStartDateTime", configFilesLocation + "\\RunTime.xml");
                //startTimeOfExecution = Convert.ToDateTime(ExecutionStartDateTime.ToString());
                //endTimeOfExecution = DateTime.Now;

                // Generate custome html report form csv logs
                AutomationReport automationReport = new AutomationReport(logFileDirectory, reportFileDirectory, server, Convert.ToDateTime(ExecutionStartDateTime.ToString()), DateTime.Now);
                embeddedMailContents = automationReport.CompileReportFromCSV();

                // Prepare path for zip file
                string zippedFolderPath = reportFileDirectory + ".zip";

                // Zip the custome html report folder
                FileZipOperations fileZipOperations = new FileZipOperations(reportFileDirectory, zippedFolderPath, null);
                fileZipOperations.ZipFiles();
            }
            catch (Exception e)
            {
                Console.WriteLine("Test Clean not succeed" + e.Message);
            }
            finally
            {

            }
        }




    }
}
  1. 我的基类是简单的非静态类,配置实用程序很少。
  2. 我的报告类是一个非常不同的非静态和简单的。
  3. 有什么方法可以从 STATIC * [ClassCleanup] *方法调用所有非静态函数和变量?或任何其他方式我可以实现相同的目标?

0 个答案:

没有答案