testfx
                                
                                 testfx copied to clipboard
                                
                                    testfx copied to clipboard
                            
                            
                            
                        ITestDataSource: access to RunSettings in method GetData
Summary
For data-driven tests, where the test-data is specified in the .runsettings (e.g. a folder with files to test) it is very difficult to generate specific test-names for each test case, that are also shown in the azure ui.
Background and Motivation
During test-discovery the method GetData does not allow accessing the TestContext of a surrounding class (since it is not a method of it), neither got a [ClassInitialize] method called, which would allow to copy the current TestContext to a static variable.
To be clear: it is the "test-data discovery" that fails (which leads to all tests getting the same name in the azure ui); the tests itself work correctly, as then the [ClassInitialize] method already got called, and I could store the TestContext to a static variable.
The only way I could solve it, was working down this list in GetData:
- get current-process
- get its parent-process
- get parent-processes command line
- search in the specified command-file for the .runsettings-file line
- read in that file and search for the data
Here is a code snipped:
                    // GetCommandLine and ParseCommandLineArguments defined elsewhere
                    var parentProcess = GetParentProcess(System.Diagnostics.Process.GetCurrentProcess().Id);
                    if (parentProcess != null)
                    {
                        string commandLine = GetCommandLine(parentProcess);
                        string[] arguments = ParseCommandLineArguments(commandLine);
                        foreach (string arg in arguments)
                        {
                            if (arg.StartsWith("@"))
                            {
                                var args_file = arg.Substring(1);
                                string[] lines = File.ReadAllLines(args_file, Encoding.ASCII);
                                foreach (var line in lines)
                                {
                                    if (line.StartsWith("/Settings:"))
                                    {
                                        var name = line.Substring("/Settings:".Length);
                                        name = name.Trim('\"');
                                        StreamReader Reader = new StreamReader(new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
                                        XmlDocument runsettings = new XmlDocument();
                                        runsettings.Load(Reader);
                                        Reader.Close();
                                        // read runsettings ...
Proposed Feature
Either an altenative GetData method which also has the TestContext as parameter, or a public static method that returns current TestContext.
Alternative Designs
See my workaround, but IMHO that very fragile and may break unexpectedly.