【问题标题】:Xunit IClassFixture Not Injected in LinuxXunit IClassFixture 未在 Linux 中注入
【发布时间】:2021-10-27 09:42:12
【问题描述】:

超级简单的测试设置:

    public class UiTests : IClassFixture<UiFixture>
    {
        private readonly IWebDriver _driver;

        public UiTests(UiFixture fixture)
        {
            _driver = fixture.Driver;
        }

        [Fact]
        public void Test()
        {
            _driver.Navigate().GoToUrl("http://localhost:5000/");

            _driver.PageSource.Should().Contain("Hello");
        }

        [Fact]
        public void Another()
        {
            _driver.Navigate().GoToUrl("http://localhost:5000/");

            _driver.FindElement(By.CssSelector("html")).Text.Should().Be("Hello World!");
        }
    }

    public class UiFixture : IDisposable
    {
        private Process _webServerProcess;

        public UiFixture()
        {
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.AddArguments("headless", "no-sandbox");
            Driver = new ChromeDriver(chromeOptions);

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                LaunchWebServer();

            WaitForWebServer();
        }

        public IWebDriver Driver { get; set; }

        public void Dispose()
        {
            if (_webServerProcess != null && !_webServerProcess.HasExited)
                _webServerProcess.Kill();
        }

        private void LaunchWebServer()
        {
            _webServerProcess = new Process
            {
                StartInfo =
                {
                    WorkingDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "..", "Web"),
                    FileName = $"dotnet.exe",
                    Arguments = "run",
                    UseShellExecute = true,
                },
            };
            _webServerProcess.Start();
        }

        private void WaitForWebServer()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var result = socket.BeginConnect("localhost", 5000, null, null);
            result.AsyncWaitHandle.WaitOne(30000, true);
            if (socket.Connected)
                socket.EndConnect(result);
            else
                socket.Close();
        }
    }

在我的 windows 盒子上运行很棒:

> dotnet test
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     2, Skipped:     0, Total:     2, Duration: 190 ms - Tests.dll (netcoreapp3.1)

但是当我在 CI 的 docker 镜像中运行时:

A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.4.3+1b45f5407b (64-bit .NET Core 3.1.10)
[xUnit.net 00:00:00.66]   Discovering: Tests
[xUnit.net 00:00:00.69]   Discovered:  Tests
[xUnit.net 00:00:00.69]   Starting:    Tests
Starting ChromeDriver 92.0.4515.107 (87a818b10553a07434ea9e2b6dccf3cbe7895134-refs/branch-heads/4515@{#1634}) on port 36909
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[xUnit.net 00:01:00.84]       System.AggregateException : One or more errors occurred. (The HTTP request to the remote WebDriver server for URL http://localhost:36909/session timed out after 60 seconds.) (The following constructor parameters did not have matching fixture data: UiFixture fixture)
[xUnit.net 00:01:00.84]       ---- OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:36909/session timed out after 60 seconds.
[xUnit.net 00:01:00.84]     Tests.UiTests.Another [FAIL]

知道出了什么问题吗?谢谢

【问题讨论】:

    标签: .net-core xunit


    【解决方案1】:

    原来这是一个误导性错误。实际上是ChromeDriver在更新fixture时出了点问题,所以我猜它不能被注入。

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多