Best way to reuse mocks across several tests? #389
-
Sorry in advance if I'm being dumb, I'm rather new to GdUnit3 and unit testing in general. I searched in the docs for a clue on this topic as well, but couldn't find anything. I have a couple of tests on a class, and I am setting up mocks for each test. There are common mocks between the two, so I've combined all my mocking logic into a separate function, and just call that function from within the tests themselves. When I'm running the tests as shown in the attached test_modal_results.gd.txt, it takes around 450ms to run each one, due to having to call the function and do the work of creating the mocks separately in each test. My (misguided?) attempts at optimizing test performanceAs an experiment, I tried a couple of things:
So it's pretty clear that I'm trying to do things I'm not really supposed to be doing, by structuring the tests in this way. However, I'd really like to cut down on that test time if possible, and I guess I'm just naturally inclined to try and reduce overhead and reuse data and references as much as possible. So my question is:Is there a preferred, recommended way of sharing and reusing mocks across multiple tests (within the same TestSuite of course) in order to improve test performance? Thanks for reading, and apologies for any headaches my shenanigans may have caused :P |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @paintedsky You should first read how a test-suite is organized => TestSuite There are two of them, For example, to create a mock that applies to all tests, you should use It is important when you create a mock in class_name MyTest
extends GdUnitTestSuite
class MyClass:
func foo() -> String:
return "foo"
var _mock :MyClass
func before():
_mock = mock(MyClass)
do_return("xxx").on(_mock).foo()
func before_test():
reset(_mock)
func test_foo():
verify(_mock, 0).foo()
_mock.foo()
verify(_mock, 1).foo()
func test_foo1():
verify(_mock, 0).foo() |
Beta Was this translation helpful? Give feedback.
Hello @paintedsky
You should first read how a test-suite is organized => TestSuite
What you need is a so called setup hook for a test suite.
There are two of them,
before
andbefore_test
.For example, to create a mock that applies to all tests, you should use
before
, since these objects exist over the lifetime of an testsuite.On the other hand,
before_test
, as the name suggests, is always called before each test and is thus freshly created and only available for the lifetime of a test case.It is important when you create a mock in
before
to reset it for each test to reset the reset the mock counted interactions.Typically it looks like this.