C # unit testing - ordered test with runsettings file

I am trying to develop a test environment with automatic regression as unit tests. Therefore, I use an ordered test to combine the various stages of testing and combine them in an easy-to-use way. For configuration purposes, I want to use the runsettings file , especially TestRunParameters. This works without errors, but only when each test runs on its own . If I run tests using ordertest , the TestContext object, which I use to access TestRunParameters, no longer contains them. I debugged the object during direct testing of the method and when testing it using ordered tests. In the first scenario, the object has the necessary properties, but when you run tests as an ordered object, the object looks completely different. Is there a difference between the two types of execution?

Various TestContext objects:

Execution as a single test image
Execution as a ordered test image

The error I am getting is the following:

Ergebnis StackTrace:    at Regression.FileSystem.FileSystemTestInitializer(TestContext context) in \FileSystem.cs:line 18 
Ergebnis Meldung:   Class Initialization method Regression.FileSystemTestInitializer threw exception. System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object..

The code executed to verify and load the settings is the following:

 namespace Regression
{
    [TestClass]
    public class FileSystem
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private static Dictionary<String, String> runSettings = new Dictionary<String, String>();

        [ClassInitialize]
        public static void FileSystemTestInitializer(TestContext context)
        {
            runSettings.Add("scenarioName", context.Properties["scenarioName"].ToString());
            runSettings.Add("testRootDir", context.Properties["testRootDir"].ToString());
        }

        [TestMethod]
        public void Action_FileSystem_CleanUp()
        {
            DirectoryInfo dir = new DirectoryInfo(runSettings["testRootDir"] + runSettings["scenarioName"] + @"\testdata\");
            foreach(FileInfo file in dir.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo childDir in dir.GetDirectories())
            {
                childDir.Delete(true);
            }

            DirectoryCopy(runSettings["testRootDir"] + runSettings["scenarioName"] + @"\testdata_backup\", runSettings["testRootDir"] + runSettings["scenarioName"] + @"\testdata\", true);
        }

The error appears on line 11, which looks like this:

runSettings.Add("scenarioName", context.Properties["scenarioName"].ToString());

Launch settings file, which is included in the visual studio test run:

<RunSettings>
  <!-- Parameters used by tests at runtime -->
  <TestRunParameters>
    <Parameter name="scenarioName" value="FileSystem_SharePointOnline_Unidirectional"/>
    <Parameter name="testRootDir" value="C:\testfiles\"/>
  </TestRunParameters>
</RunSettings>
+4

Source: https://habr.com/ru/post/1654429/


All Articles