在我的 xunit 测试中,为什么 运行-once 代码 运行ning 两次?

In my xunit tests, why is this run-once code running twice?

我有 3 个测试需要相同的临时文件设置。在 this answer 之后,我实现了一个集合,尝试进行一次设置和删除。它不工作。 setup 和 takedown 块中的断点会触发两次,我必须使用 ifs 来保护该块。为什么两次,当我有三个测试?我想让它开火一次。三个我能理解,但为什么两次?

以下代码将显示问题。我已经疏散了测试并将保护性的 ifs 放在评论中。如果您在 File.WriteAllText 和 File.Delete 中放置断点,您应该会看到它们分别触发两次,并且第二次命中 File.Delete 将产生异常。

using FluentAssertions;
using System;
using System.IO;
using Xunit;

namespace Tests
{
    [Collection("FileSystemHelper Collection")]
    public class FileSystemHelperShould : IClassFixture<FileFixture>
    {
        FileFixture _fileFixture;

        public FileSystemHelperShould(FileFixture fileFixture)
        {
            _fileFixture = fileFixture;
        }
        [Fact]
        public void WhenCalledWithMangledFilePath_ReturnCorrectCapitalization()
        {
            // test goes here
        }

        [Fact]
        public void WhenCalledWithMangledFolderPath_ReturnCorrectCapitalization()
        {
            // test goes here
        }
        [Fact]
        public void WhenCalledAsExtensionMethod_ReturnCorrectCapitalization()
        {
            // test goes here
        }
    }
    [CollectionDefinition("FileSystemHelper Collection")]
    public class FileHelperCollection : ICollectionFixture<FileFixture>
    {
        // empty class defines the collection
    }
    public class FileFixture : IDisposable
    {
        public string TempDir { get; set; }
        public string NewFolder { get; set; }
        public string NewFile { get; set; }
        public FileFixture()
        {

            TempDir = Path.GetTempPath();
            NewFolder = Path.Combine(TempDir, "NeWfoLder");
            NewFile = Path.Combine(NewFolder, "NewfIlE.Txt");
            //if (!Directory.Exists(NewFolder))
                Directory.CreateDirectory(NewFolder);
            //if (!File.Exists(NewFile))
                File.WriteAllText(NewFile, "Hello Cobber");
        }

        public void Dispose()
        {
            try
            {
                //if (File.Exists(NewFile))
                    File.Delete(NewFile);
                //if (Directory.Exists(NewFolder))
                    Directory.Delete(NewFolder);
            }
            catch (Exception) { }
        }
    }
}

这是一个未解决的 GitHub 问题,标记为错误

https://github.com/xunit/xunit/issues/2268