1

我正在尝试调用 [OneTimeSetUp],但它没有调用它并直接调用 [TestFixture] SetUp。下面是我正在使用的包

在.NetFramework v4.X中工作正常 **

<PackageReference Include="NUnit" Version="3.13.1" /> 
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />

**

namespace Test.BaseClass
{

    [SetUpFixture]
    public class SetUpClass
    {
        public static ExtentReports extent;

        [OneTimeSetUp]
        public void RunBeforeAnyTests()
        {
            extent = new ExtentReports();

            string workingDirectory = Directory.GetCurrentDirectory();
            string basedir = Directory.GetParent(workingDirectory).Parent.Parent.FullName;
            string path = AppSettings.Settings[$"Log:FileLocation"];
            string dir = basedir + path;
            var htmlReporter = new ExtentHtmlReporter(dir);
            htmlReporter.Config.DocumentTitle = "Automation Testing Report";
            htmlReporter.Config.ReportName = "Regression Testing";
            htmlReporter.Config.Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Standard;

            extent.AttachReporter(htmlReporter);
            extent.AddSystemInfo("Application Under Test", TestContext.CurrentContext.Test.ClassName);
            extent.AddSystemInfo("Environment", AppSettings.env);
            extent.AddSystemInfo("Machine", Environment.MachineName);
            extent.AddSystemInfo("OS", Environment.OSVersion.VersionString);
        }

        [OneTimeTearDown]
        public void RunAfterAnyTests()
        {
            extent.Flush();
        }
    }
}

这是[设置]

namespace Test.BaseClass
{
     
    public class BaseTest
    {
        public static ExtentReports extent;
        public ExtentTest test;
        
        [SetUp]
        public void StartUpTest()
        {
            test = SetUpClass.extent.CreateTest(TestContext.CurrentContext.Test.Name);
            DriverHelper.InitBrowser();
            DriverHelper.BrowserMaximize();
            DriverHelper.DeleteAllCookies();
        }
        [TearDown]
        public void AfterTest()
        {
            try
            {
                var status = TestContext.CurrentContext.Result.Outcome.Status;
                var stacktrace = TestContext.CurrentContext.Result.StackTrace;
                var errorMessage = "<pre>" + TestContext.CurrentContext.Result.Message + "</pre>";
                Status logstatus;
                switch (status)
                {
                    case TestStatus.Failed:
                        logstatus = Status.Fail;
                        DateTime time = DateTime.Now;
                        String fileName = TestContext.CurrentContext.Test.Name;
                         
                        test.Log(Status.Fail, "Fail");
                         
                        break;
                    case TestStatus.Inconclusive:
                        logstatus = Status.Warning;
                        break;
                    case TestStatus.Skipped:
                        logstatus = Status.Skip;
                        break;
                    default:
                        logstatus = Status.Pass;
                        break;
                }
                test.Log(logstatus, "Test ended with " +logstatus + stacktrace);
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                DriverHelper.Close();
            }
        }
    }
}

当我运行测试时,它会跳过 [SetUpFixture] 并直接调用 [TestFixture] 我已经尝试将两者放在单独的命名空间中

4

1 回答 1

0

从 NUnit文档中,您需要Microsoft.NET.Test.Sdk.Net Core安装。我尝试了它执行的演示项目OneTimeSetUpOneTimeTearDown方法。您可以在以下解决方案中进行检查。但是日志记录有一个错误。你可以从github看到这个问题。

namespace ConsoleApp1
{
    [SetUpFixture]
    public class SetUpClass
    {
        [OneTimeSetUp]
        public void RunBeforeAnyTests()
        {
            TestContext.Progress.WriteLine("One Time SetUp");
        }

        [OneTimeTearDown]
        public void RunAfterAnyTests()
        {
            TestContext.Progress.WriteLine("One Time TearDown");
        }
    }
}
namespace ConsoleApp1
{
    [TestFixture]
    public class BaseTest
    {
        [TearDown]
        public void Teardown()
        {
            Console.WriteLine("teardown");
        }
    }
}
namespace ConsoleApp1
{
    public class DemoTest : BaseTest
    {
        [SetUp]
        public void SetUp()
        {
            Console.WriteLine("setup");
        }

        [Test]
        public void Test()
        {
            Assert.True(true);
        }
    }
}

输出 :

setup
teardown
于 2021-03-31T22:03:46.780 回答