Forum Discussion
It would help if you provide your Python script to look at.
Root Cause Analysis
TestComplete’s Python engine loads each script unit only once per project session. Any references you make to ProjectSuite.Variables, Project.Variables or Log.Message at the module (global) scope execute at import time—and then stay cached.
- On first run, the module is imported, the global references succeed, and everything works.
- On subsequent runs, TestComplete reuses the already-loaded module and does not re-evaluate module-level code. Your stale or missing references now trigger “object not found.”
- Likewise, calling Log.Message outside of a test routine or after import can silently fail or not bind to the TestComplete test engine context.
Step-by-Step Solution
- Move all variable reads and log calls inside a Python function or test routine—never at the top-level of the script unit.
- Use the full object model for each variable kind:
- Suite variables: ProjectSuite.Variables.YourSuiteVar
- Project variables: Project.Variables.YourProjectVar
- Keyword-test variables: pass them as parameters when you invoke your Python routine from a Keyword Test.
- Inside your routine, wrap your variable lookups in a try/except to catch missing-object errors and report them via Log.Error().
- Ensure you invoke your function from a Python Script Test or call it via a Keyword Test each time you need it. This guarantees the code block that reads variables and logs runs fresh on every execution.
- (Optional) If you ever need to force-reload your module after editing, you can use Python’s importlib.reload(), but well-structured functions typically make that unnecessary.
Code Snippet
With this pattern, every time you run the Test routine—whether back-to-back or after many runs—the engine will (re)execute ReadAndLogVariables(), freshly resolve your variables, and correctly bind Log.Message() to the active test context.
# Put this in your Script Unit, not at module top-level
def ReadAndLogVariables():
# Dynamically fetch suite & project variables
try:
suite_var = ProjectSuite.Variables.MySuiteVariable
project_var = Project.Variables.MyProjectVariable
except AttributeError as e:
Log.Error("Variable access failed: " + str(e))
return
# Log the values so you can verify each run
Log.Message("Suite variable value: {}".format(suite_var))
Log.Message("Project variable value: {}".format(project_var))
# Your Python Script Test entry point
def Test():
ReadAndLogVariables()
💬 If a response helped you out, don’t forget to Like it! And if it answered your question, mark it as the solution so others can benefit too.
Note: This reply was drafted with AI assistance, and as I do not have access to license I did not try out such Code Snippet.
Thanks for your rapid reply. Here is a step-by-step of what is happening. This is the keyword test Sandbox:
This is the Keyword test variables:
Here are the project level variables:
Here is the code for the script CheckForEnvVariables in the file LoginScripts:
def CheckForEnvVariables():
for i in range(KeywordTests.Sandbox.Variables.VariableCount):
name = KeywordTests.Sandbox.Variables.GetVariableName(i)
Log.Message("Name: " + str(name))
Log.Message("Value: " + str(KeywordTests.Sandbox.Variables.VariableByName[name]))
for i in range(Project.Variables.VariableCount):
name = Project.Variables.GetVariableName(i)
Log.Message("Name: " + str(name))
Log.Message("Value: " + str(Project.Variables.VariableByName[name]))
After bringing up TestComplete cold, the first thing I do is run the keyword test Sandbox. This is the resulting log file, which looks like what we want:
I immediately run Sandbox a second time and this is the error I get:
And this is the resulting log file:
And further attempts to run Sandbox have the same error
- rraghvani3 months ago
Champion Level 3
From TC v15.68.8.7 and onwards, there's been a number of reported issues, when using Log.Message method in Python, as well as other issues!
I suggest you open a support ticket via https://support.smartbear.com/testcomplete/message/
Note, use the Formatted string literals to simplify string formatting and interpolation e.g.
def CheckForEnvVariables(): for i in range(0, Project.Variables.VariableCount): name = Project.Variables.GetVariableName(i) Log.Message(f"Name: {name}") Log.Message(f"Value: {Project.Variables.VariableByName[name]}")
- TikiHardBop13 months agoNew Contributor
I have opened a support ticket for the Log.Message() stuff (#759774). I have not heard back. I was just wondering if this problem with variables is related or a separate issue. I will ask them when I hear back about the Log.Message() support ticket.
I'm curious how a bug with something as basic as a Log.Message() would even make it into a final released product, though?
- rraghvani3 months ago
Champion Level 3
If you search the forum, you will come across similar issues.
I'm not sure if the Python libraries that's packaged with TestComplete have been updated, and is now causing these issues.