No announcement yet.

Question about Automation Spec for Unit Tests

  • Filter
  • Time
  • Show
Clear All
new posts

    Question about Automation Spec for Unit Tests

    I've been working an a suite of unit tests for something using the Automation Spec feature:

    It seems like it's not possible to declare variables in the body of a Describe Lambda. The pointers become garbage values if stepping through with a debugger.

    Describe("Some describe block", [this]()
        USomeClass * SomePtr0;
        USomeClass * SomePtr1;
        BeforeEach([this, &SomePtr0, &SomePtr1]()
            //Set up test objects
            SomePtr0 = NewObject<USomeClass>(USomeClass::StaticClass());
            SomePtr1 = NewObject<USomeClass>(USomeClass::StaticClass());
        It("should use the test objects", [this, SomePtr0, SomePtr1]()
            TestEqual("Yeah, are these equal?", SomePtr0->GetSomeVar(), 4);
            TestEqual("Yeah, are these equal? What a useful test.", SomePtr1->GetSomeVar(), 5);
    In this example, I create SomePtr0, and it is pointing to a valid class. On the next line I create another class and assign to SomePtr1, and SomePtr0 is instantly freed and the pointer is now pointing to 0xcccccccccccccccc or whatever the visual studio value is for a freed pointer.

    Then on the line where I run SomePtr0->SetSomeVar(5); SomePtr1 is instantly freed too, and the application crashes because I'm dereferencing SomePtr0 which is now garbage.

    I'm pretty sure I know my C++ well enough to know that I wrote the lambda syntax correctly so SomePtr0 and SomePtr1 go into the lambda as references and should be usable in the subsequent tests. But it's possible I'm misunderstanding something about C++ lambda syntax here?

    My other suspicion is I'm not understanding how the Automation Spec code works. Maybe it's designed in a way where we shouldn't declare variables in the Describe block? It's not mentioned in the docs on how to write Specs, but as I understand it, the engine runs these lambdas to figure out what tests to eventually run and display in the Automation tool. So maybe it's actually going to break things if we declare variables in the Describe block? It seems like it would be very useful to be able to, so we can declare variables for a specific scope, but maybe they don't actually live with an instance of a running test like I thought? If that's the case, there should be a warning added to the doc page so people know that's a limitation.

    Also, on a side note, there's a bug in the automation tool in 4.22. Tests show twice, until you hit refresh tests.
    Last edited by illYay; 07-23-2019, 04:19 AM.