4. Writing Resource Classes

Writing resource classes is similar to writing test classes. The requirements are the same except that, instead of a Run method, you must provide two methods named SetUp and CleanUp. The SetUp method must have the same signature as a test classs Run. The CleanUp method is similar, but does not take a context parameter.

The SetUp method may add additional properties to the context by assigning to its context parameter. These additional properties will be visible only to tests that require this resource.

The example below shows the SetUp and CleanUp from the standard QMTest TempDirectoryResource class. This resource creates a temporary directory for use by the tests that depend on the resource. The SetUp method creates the temporary directory and records the path to the temporary directory in the context so that tests know where to find the directory. The CleanUp method removes the temporary directory.

 
 
    def SetUp(self, context, result):

        # Create a temporary directory.
        self.__dir = qm.temporary_directory.TemporaryDirectory()
        # Provide dependent tests with the path to the new directory.
        context["TemporaryDirectoryResource.temp_dir_path"] 
          = self.__dir.GetPath()
    

    def CleanUp(self, result):

        # Remove the temporary directory.
        del self.__dir