Forum Discussion
I do something similar in Python. The problem is, Python retains the singleton between test items. If I run 10 test items, the singleton only gets created once. This is normally how I want python to work.
But, I have a specific case where I need to reset some values but only when running a new test item.
You could use TestComplete Variables, but that just tracks state β it doesnβt solve the real issue, which is that Python persists for the entire run in TestComplete.
A cleaner approach is to reset your singleton at the start of each Test Item using a project event.
Do this:
- Go to Project β Events
- Add an OnStartTest handler
- Reset your class there
Example:
def GeneralEvents_OnStartTest(Sender):
MySingleton.reset()
And in your class:
class MySingleton:
_instance = None
@classmethod
def reset(cls):
cls._instance = None
Since OnStartTest fires for every Test Item, this guarantees your setup runs once per Test Item without changing how the class is used elsewhere.
π€ AI-assisted response
π Found it helpful? Click Like
β
Issue resolved? Click Mark as Solution
- mfoster71119 days agoRegular Contributor
This works. I was thinking there would be a more Python specific solution but this is really a TestComplete issue and TestComplete solution.
- rraghvani16 days ago
Champion Level 3
I wouldn't recommend setting the singleton instance to None just to reset its variables. That effectively breaks the singleton guarantee and alters its lifecycle semantics.
If the goal is to clear state (for example, between tests), it's cleaner to provide a method on the singleton that resets its internal variables. This keeps the instance intact while preserving the intent of the pattern.
You can then call the method during either OnStartTest / OnStopTest or OnStartTestCase / OnStopTestCase event to reset the variables.