Forum Discussion

mfoster711's avatar
mfoster711
Regular Contributor
20 days ago

Check if running next test item

I have a Python Class that gets called from numerous places in our code. In that class, I have check to determine if this is the first time to call the class and it will perform some setup code if it is. Otherwise, on subsequent calls to the class it will skip the code.

This code works fine as long as I am running one test item. If I have multiple test items, Python will see the class as still being initialized but I need it to perform the setup code again. 

Is there a way to set a variable in python that gets reset each time I go to a new TestComplete Test Item?

7 Replies

  • mfoster711's avatar
    mfoster711
    Regular Contributor

    Here is some more information.

    I have the following two test items in TestComplete. Both run the same thing.

    The first test runs fine. I added some logging to highlight where the setup code ran vs where it was skipped.

     

    When the second test item runs, it skips the setup code but I need it to run the setup. 

    The error that occurred is a result of my setup code not running.

    I need this setup code to run once for each test item.

  • scot1967's avatar
    scot1967
    Icon for Champion Level 3 rankChampion Level 3

    Hi mforester711.

    Would an project variable set in TestComplete to the job?  These can be called from a script,  set and reset and can persist through out a test run.   

    https://support.smartbear.com/testcomplete/docs/testing-with/variables/collections/project-and-project-suite/index.html

    Double click your project or suite to access them.

    ... If you find my posts helpful drop me a like! ๐Ÿ‘ Be sure to mark or post the solution to help others out and to credit the one who helped you. ๐Ÿ˜Ž

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    In JavaScript, I've created a Singleton Class which gets instantiated once, and is called in every test. It reads a configuration file i.e. Storages.INI method, and sets up appropriate parameters, variables etc. While the scripts are running, only one instance is created, and if I create a new instance of the singleton class, the same object is returned.

    The same can be applied in Python, see The Singleton Pattern as an example

    • mfoster711's avatar
      mfoster711
      Regular Contributor

      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. 

      • Hassan_Ballan's avatar
        Hassan_Ballan
        Icon for Champion Level 3 rankChampion Level 3

        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:

        1. Go to Project โ†’ Events
        2. Add an OnStartTest handler
        3. 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