Matt W
Matt W

Reputation: 12434

Run Settings for XUnit tests

The dotnet test documentation page states that arguments can be passed as pairs to unit tests.

RunSettings arguments

Arguments passed as RunSettings configurations for the test. Arguments are specified as [name]=[value] pairs after "-- " (note the space after --). A space is used to separate multiple [name]=[value] pairs.

I am using Xunit - How do I read these key/value pairs in my test code?

Upvotes: 4

Views: 2761

Answers (1)

Matt W
Matt W

Reputation: 12434

I have solved this by using the Environment class to directly read the command line parameters explicitly (note: incomplete at time of writing):

public class Parameters
{
    public static string GetCommandLineArg(params string[] args)
        => Environment.GetCommandLineArgs().SkipWhile(x => !args.Contains(x, StringComparer.InvariantCultureIgnoreCase)).Skip(1).FirstOrDefault();

    public static string ResultsDirectory => GetCommandLineArg("-r", "--result-directory");
    public static string Logger => GetCommandLineArg("-l", "--logger");
}

Upvotes: 1

Related Questions