Skip to content

Error when a slash.Test class in python inherits from a singleton Class slash framework can not run mutiple tests #1081

@Kayhan-lab

Description

@Kayhan-lab

We have a Singleton Class called GeneralEnvInfo :

class SingletonMeta(type):
"""
This is a metaclass that ensures there's only one instance of Singleton.
"""
_instances = {}

def __call__(cls, *args, **kwargs):
    if cls not in cls._instances:
        instance = super().__call__(*args, **kwargs)
        cls._instances[cls] = instance
    return cls._instances[cls]

class GeneralEnvInfo(metaclass=SingletonMeta):
_shared_state = {}

def __new__(cls, *args, **kwargs):
    obj = super(GeneralEnvInfo, cls).__new__(cls)
    obj.__dict__ = cls._shared_state
    cls.initialized = False
    return obj

def __init__(self):
    if not self.initialized:
        print("Initializing GeneralEnvInfo instance")
        self.home_door = False
        self.available_aps = []
        self.default_ssid = "SSID1"
        netgear_present = True
        self.provisioned_ssids = []
        if not netgear_present:
            self.available_aps.append(self.default_ssid)
        self.connected_ssid = None
        self.home_wifi = True
        self.wifi_sta_mode = True
        self.wifi_connected = False
        self.verdict = None
        self.initialized = True
        print("GeneralEnvInfo instance initiated")

now we have test classes defined as below :

from .singleton2 import *

class MyState(GeneralEnvInfo):
def init(self):
super().init()
print("CarState Class Init")
self.home_door = None
self.wifi = None

def door_open(self):
    self.home_door = False

def door_close(self):
    self.home_door = True

def home_wifi_off(self):
    self.home_wifi = False

def home_wifi_on(self):
    self.home_wifi = True

class SingleFixture:
def init(self):
print("SingleFixture Class Init")
self.state = MyState()

def start_test(self):
    print("SingleFixture Class Test Start")

def end_test(self):
    print("Test End")

@slash.fixture
def single_test_fixture(this):
@this.add_cleanup
def scope_cleanup():
if 'single_test_env' in this.dir():
print("Start Cleaning Up single_test_fixture")
print("single_test_fixture fixture Cleaned up")

@this.test_start
def on_test_start():
    """Test Start fixture"""
    print("single_test_fixture start")
    this.single_test_env.start_test()

@this.test_end
def on_test_end():
    """Test End fixture """
    print("single_test_fixture end")
    this.single_test_env.end_test()

print("single_test_fixture start")
this.single_test_env = SingleFixture()
return this.single_test_env

class WlanClass(GeneralEnvInfo):
def init(self):
super().init()
#self.general_info = GeneralEnvInfo()
print("WlanClass Init")

class WlanTest(WlanClass):
def init(self):
super().init()
#WlanClass.init(self)
self.new_values = "Test"
self.state: MyState = None
print("WlanTest Class Init")

class TestCase(slash.Test, WlanTest):

def __init__(self, test_method_name, fixture_store, fixture_namespace, variation):
    super().__init__(test_method_name, fixture_store, fixture_namespace, variation)
    print("Initializing TestCase")

def before(self, single_test_fixture):
    print("Before of WlanTest")
    super().before()
    self.home = single_test_fixture.state
    self.home.door_close()
    self.home.home_wifi_off()

def after(self):
    print("After of WlanTest")
    super().after()

def test_example1(self):
    print("test_example1 start")
    self.home.door_open()
    self.home.home_wifi_off()
    new_env = GeneralEnvInfo()
    print("General info Wifi State: {0}".format(new_env.home_wifi))
    print("General Info Door Status: {0}".format(new_env.home_door))


def test_second_example(self):
    self.home.door_open()
    self.home.home_wifi_off()
    new_env = GeneralEnvInfo()
    print("General info Wifi State: {0}".format(new_env.home_wifi))
    print("General Info Door Status: {0}".format(new_env.home_door))

if there is only one test run , it will pass but collecting multiple tests returns error :

(FNV2-CM) c:\Git\FNV2-CM>slash run C:\Users\nettest\Local\Local\test_scripts\example_fixture.py -vvv
ldf is not supported
xls is not supported
xlsx is not supported
Initializing GeneralEnvInfo instance
GeneralEnvInfo instance initiated
WlanClass Init
WlanTest Class Init
Initializing TestCase
Session error caught -- <Error: slash.exceptions.SlashInternalError: INTERNAL ERROR:
<Runnable test <C:\Users\nettest\Local\Local\test_scripts\example_fixture.py:TestCase.test_example1>> has Metadata before generating it
Please open issue at: https://github.com/getslash/slash/issues/new>

[2024-06-21 21:32:50] Saved resume state to DB
ERROR: INTERNAL ERROR:
<Runnable test <C:\Users\nettest\Local\Local\test_scripts\example_fixture.py:TestCase.test_example1>> has Metadata before generating it
Please open issue at: https://github.com/getslash/slash/issues/new

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions