How to implement a new test?

Since the Testing Framework requires the existence of a plugin and possibly a parametrization file, here we will show how to create two different kinds of plugins. Both plugins are just an "Hello World" example where the first does not demand configuration file, otherwise the second needs a valid file for parameterization.

Hello World

In this first example, the config file is not needed and the presented code is only a demo to show the requisites and functionalities for implementing a new plugin. We must create a new file named as helloworld.py inside the hapi/test/plugins directory.

from testable import Testable
from testable import Status

class Test(Testable):
  def __init__(self):
    Testable.__init__(self)

  def run(self):
    self._state['result'] = "Hello World"
    self._state['status'] = Status.VALID

  def result(self):
    print(self._state)

To execute helloworld.py, in the hapi/test/ directory type:

foo@bar:~/hapi/test$ python3 main.py -helloworld None

The None word denotes that the plugin does not use a configuration file.

There are three mandatory steps on the new plugin development that not demands an initial file to parametrize the test:

  1. Extend the testable.Testable class with a new class named Test.
  2. Implement the abstract method run().
  3. Implement the abstract method result().

The goal is to implement completely the test responses made in the run() stage and the result() will be used just to manipulate the output.

Hello Folks

In the second plugin, we show how to use a configuration file for structuring a more elaborate test script. Here a YAML file (config.yml) is used, however the Testing Framework also supports JSON format.

Once config.yml is in hapi/test/fixtures/configs and contains an array with some names, the response of our test will be given as Hello <name> for each item listed:

- Anna
- Bernard
- Curry
- Daniel
- Ernest

Our plugin is also inside hapi/test/plugins and this one is named as hellofolks.py as well as the helloworld.py. So we have the following code:

from testable import Testable
from testable import Status
import yaml, sys

class Test(Testable):
  def __init__(self):
    Testable.__init__(self)

  def run(self):
    self._state['result'] = {}
 
    # self._config: config file
    for name in self._config:
        local_result = {
            'message': 'Hello '+name,
            'status': Status.VALID
        }
        self._state['result'][name] = local_result

    self._state['status'] = Status.VALID

  def result(self):
    yaml.dump(self._state['result'], sys.stdout, default_flow_style=False)

Two main point that we should pay attention:

  1. The testable.Testable._config attribute contains the config.yaml data, then as the hellofolks plugin extends this class, it can catch this data accessing self._config.
  2. In result() the formatting is optional by the developer side, so in this example we have decided to print the result with the YAML format.

Lastly the execution is given by:

foo@bar:~/hapi/test$ python3 main.py -hellofolks fixtures/configs/config.yml