0

It seems like the NUnit test runner gets confused if I try to use a randomized temporary directory name in my parametrized test.

The test looks roughly like

[TestCaseSource(nameof(InputFiles))]
        public void TestMain(string inputFile, string outputDirectory)
        {
            var args = new string[] { $"-i={inputFile}", $"-o={outputDirectory}" };
            var fp = new FileProcessor();
            var exitcode = fp.Read(args);
            Assert.AreEqual(exitcode, 0);
        }

        static IEnumerable<string[]> InputFiles()
        {
            var matcher = new Matcher();
            matcher.AddInclude("**/*.lcd");
            matcher.AddInclude("**/*.lcb");

            string workingDirectory = Environment.CurrentDirectory;
            var rootDirectory = Directory.GetParent(workingDirectory).Parent.Parent.Parent;
            var searchDirectories = rootDirectory.GetDirectories("example-input");

            if (searchDirectories.Length != 1) { throw new Exception("Unexpected number of samples directories"); }
            var samplesDirectory = searchDirectories[0];

            var patternMatch = matcher.Execute(new DirectoryInfoWrapper(samplesDirectory));
            foreach (var file in patternMatch.Files)
            {
                var inputDir = Path.Combine(rootDirectory.FullName, "example-input", file.Path);
                // This works fine if I use something like 
                // var outputDir = Path.Combine(Path.GetTempPath(), "example-output", file.Stem, @"..");
                var outputDir = Path.Combine(Path.GetTempPath(), $"Test_{DateTime.Now}", "example-output", file.Stem, @"..");
                yield return new string[] { inputDir, outputDir };
            }
        }

If I try to run it, the test runner seems to get confused and doesn't run any tests.

Test runner refuses to run tests

The only indication that test discovery is an issue is this message I get in Tests output

The problem does not seem to occur if the parameter is not randomized, so if I only use GetTempPath() but don't have use a DateTime.Now in my path.

It seems like this may be a known issue in NUnit: https://github.com/nunit/nunit3-vs-adapter/issues/972

I would like to generate a randomized directory to put test results in. What are my options here?

  • From the issue you linked: "Between execution discovery and execution run there will be a time difference and using a testcasesource means that the data actually becomes part of the name of the test. When these doesn't match it will fail to discover anything." Why do you want to generate a randomized directory to put test results in? Why do you want that directory to be incorporated into the identity of your test upon test discovery, rather than being generated as the test is run? – StriplingWarrior Apr 26 '23 at 15:09

0 Answers0