<TestPlanDir> stays TBD in file path macro string

I have a test step with one of the parameters being a file path. I’d like this path to be relative to the test plan directory, so my parameter is of MacroString type and I’m using <TestPlanDir> as part of the path.

In my step I use Expand() method to render the path but I see that <TestPlanDir> expands to TBD even though my test plan is saved to disk.

I checked the implementation of Test Plan Reference test step and found out that <TestPlanDir> expansion is handled very explicitly there. Is it something I have to do in my test step as well? I was under the impression <TestPlanDir> is supported by macro strings out of the box or is it only applicable to result listeners?

1 Like

Looking at the unit tests we have, it seems that TestPlanDir should work for all test steps (as long as they are in a saved test plan). I think you need to set MacroString.Context to ‘this’ inside the test step for TestPlanDir to work. Otherwise the MacroString class cannot resolve the test plan to get access to that macro value.

For example you can initialize the MacroString like this:

PathToThing = new MacroString(this) {Text = "<TestPlanDir>/thing"};
1 Like

Thanks, that worked. I thought it’s enough to pass planRun when expanding but apparently it’s not the case.

1 Like

Actually, that should also work. At least, this test that I made passes.

        [Test]
        public void MacroStringTestPlanDirFromArgument()
        {
            var filename = Path.Combine(Path.GetTempPath(), "OpenTapTest", Guid.NewGuid() + ".TapPlan");
            Directory.CreateDirectory(Path.GetDirectoryName(filename));
            var plan = new TestPlan();
            plan.Save(filename);
            var planRun = plan.Execute();
            File.Delete(filename);

            var expandedString = new MacroString() { Text = "<TestPlanDir>\\test.x" }.Expand(planRun);
            Assert.AreEqual(Path.Combine(Path.GetTempPath(), "OpenTapTest",  "test.x"), expandedString);

        }

When do you call ‘expand’ to be precise?

1 Like