0

I have a test suite for testing some powershell cmdlets, the problem is that if i run each test independently i will have the proper results but if i run the whole suite im getting information from other tests.

i have a class that has a private read only dictionary field

in that class i have a hook that gets executed before every test scenario that creates a runspace and adds it to the dictionary for use later.

i also have a hook for after each scenario it will get the runspace from the dictionary and dispose of it.

during the tests i add the output object that is returned from the following code to the dictionary so i can do my asserts on it in the following steps.

The problem is that if i run a cmdlet that will create a new profile with the name "bob" in one test and then run a cmdlet to get the profile in another test where no profile has ever been created for some reason the output will continue to contain the bob profile from another test

i have made sure all the hooks are indeed working as intended everything gets disposed properly

    public static PowershellOutput ExecutePowershellCommand(Runspace runspace, string commandName, IEnumerable<CommandParameter> commandParameters, string runspaceVariableName = null)
    {
        var output = new PowershellOutput();
        using (var pipeline = runspace.CreatePipeline())
        {
            var command = new Command(commandName);
            foreach (var commandParameter in commandParameters)
            {
                command.Parameters.Add(commandParameter);
            }
            pipeline.Commands.Add(command);
            output.Errors = new List<ErrorRecord>();
            try
            {
                output.Result = pipeline.Invoke();
                if (!string.IsNullOrEmpty(runspaceVariableName))
                {
                    runspace.SessionStateProxy.SetVariable(runspaceVariableName, output.Result[0].BaseObject);
                }
            }
            catch (Exception e)
            {
                output.Errors.Add(new ErrorRecord(e, string.Empty, ErrorCategory.InvalidOperation, e));
            }

            if (pipeline.Error.Count <= 0)
            {
                return output;
            }

            while (!pipeline.Error.EndOfPipeline)
            {
                var value = pipeline.Error.Read() as PSObject;

                var currentErrorRecord = value?.BaseObject as ErrorRecord;
                if (currentErrorRecord != null)
                {
                    output.Errors.Add(currentErrorRecord);
                }
            }
        }
        return output;
    }

when i run the get profile cmdlet with no profile having been made in that tests context it should not return any profile

m47791
  • 13
  • 3

0 Answers0