0

我将 NUnit 用于 Selenium C# 项目。其中我有很多测试方法。为了获取数据(来自excel),我使用了一个公共静态方法,IEnumerable<TestCaseData>该方法返回我在测试方法级别调用的TestCaseSource。我现在面临挑战,因为我开始在测试方法上执行它正在调用项目中的所有静态方法。

代码如下所示:

    public static IEnumerable<TestCaseData> BasicSearch()
        {
            BaseEntity.TestDataPath = PMTestConstants.PMTestDataFolder + ConfigurationManager.AppSettings.Get("Environment").ToString() + PMTestConstants.PMTestDataBook;
            return ExcelTestDataHelper.ReadFromExcel(BaseEntity.TestDataPath, ExcelQueryCreator.GetCommand(PMTestConstants.QueryCommand, PMTestConstants.PMPolicySheet, "999580"));
        }


        [Test, TestCaseSource("BasicSearch"), Category("Smoke")]
        public void SampleCase(Dictionary<string, string> data)
        {
            dosomething;         
        } 

有人可以帮助我如何将我的数据调用方法限制为相应的测试方法吗?

4

1 回答 1

0

Your TestCaseSource is not actually called by the test method when you run it, but as part of test discovery. While it's possible to select a single test to execute, it's not possible to discover tests selectively. NUnit must examine the assembly and find all the tests before it's possible to run any of them.

To make matters worse, if you are running under Visual Studio, the discovery process takes place multiple times, first before the tests are initially displayed and then again each time the tests are run. This is made necessary by the architecture of the VS Test Window, which runs separate processes for the initial disovery and the execution of the tests.

That makes it particularly important to minimize the amount of work done in test discovery, especially when running under Visual Studio. Ideally, you should structure the code so that the variable parameters are recorded during discovery. The actual data access should take place at execution time. This can be done in a OneTimeSetUp method, a SetUp method or at the start of the test itself.

Finally, I'd say that your instinct is correct: it should be possible to set up a TestCaseSource, which only runs if the test you select is about to be executed. Unfortunately, that's a feature that NUnit doesn't yet have.

于 2020-01-03T13:38:09.103 回答