Stateful Testing
This section aims to explain how the stateful testing of Kea is designed and implemented.
Functional Description and Design
The Bundle class is the core class of stateful testing. It is primarily responsible for recording the combinations of related states and operations of multiple groups of different types of data, used to test the behavior of the system under different states. This class provides complete member methods for CRUD operations of the state. The main methods included in Bundle are:
Determine whether a new state needs to be added based on the current status of the data type (singleton pattern).
CRUD operations on the state of a certain type of data.
Randomly generate status text values of specified length.
Randomly retrieve a state of a certain type of data.
Composition of the Bundle Class
Implementation of data structures in the Bundle class
_bundle_
_bundle_ is a class variable of Bundle, which serves as a state record library for various types of data in stateful testing, used to store application states for easy manipulation of the states. Each data item is stored as a <string, Bundle> key-value pair.
data_name
data_name is of string type and stores the name of a type of data.
data_value
data_value is of list type and stores all states of the data of that type.
Note
To facilitate reader understanding, the code snippets provided in this article are simplified versions that abstract and display only the core processes, and the actual code may not fully align with the simplified reference code.
Implementation of functional methods in the Bundle class
Singleton Pattern Method
__new__
Determine whether a Bundle object of the current data type has been instantiated based on the name of the current data type. If it has not been instantiated, instantiate the Bundle object of that type and return it. Otherwise, do not instantiate and return the previously instantiated object.
- Parameters:
data_name: The name of the type of data to instantiate.
- Returns:
An instance of that type of data.
def __new__(cls, data_name: str = None): if data_name in cls._bundles_: return cls._bundles_[data_name] else: instance = super().__new__(cls) cls._bundles_[data_name] = instance return instance
Member Methods for CRUD Operations on Data States
add
Add a state for the current type of data.
- Parameters:
value: The added state value.
def add(self, value = None): if value is None: raise ValueError("the value of " + self.data_name + " cannot be None") self.data_value.append(value)
delete
Delete a state for the current type of data.
- Parameters:
value: The state value to be deleted.
def delete(self, value = None): if value is None: raise ValueError("the value of " + self.data_name + " cannot be None") self.data_value.remove(value)
update
Update the state for the current type of data.
- Parameters:
value: The old state value to be modified.new_value: The new state value.
def update(self, value = None, new_value = None): if new_value is None: raise ValueError("the new name of " + self.data_name + " cannot be None") if value is None: raise ValueError("the old name of " + self.data_name + " cannot be None") try: self.data_value.remove(value) self.data_value.append(new_value) except KeyError: print(f"'{value}' is not a object of Bundle.")
get_all_data
Retrieve all states of that type of data.
- Returns:
List of States
def get_all_data(self): return self.data_value
Member Method to Randomly Generate Status Text
get_random_text
Randomly generate status text values of specified length.
- Parameters:
value_max_len: The maximum length of the required status text value.
- Returns:
Valid Status Text Values
def get_random_text(self, value_max_len = 10): text = st.text(alphabet=string.ascii_letters, min_size=1, max_size=value_max_len).example() return text
Member Method to Randomly Retrieve a State
get_random_data
Randomly retrieve a state of a certain type of data.
- Returns:
A state value of that type of data.
def get_random_data(self): random_item = random.choice(self.data_value) return random_item