Feature testing¶
Cattle_grid is feature tested using behave. The packages below contain useful general functions. Individual steps are described in Steps.
Use in your project¶
The tools provided here are meant to be reusable. Generally, the aim is to use almabtrieb to talk to a compatible server. Then one can use the steps and tools provided here to write tests. I have done this a few times, see comments and its feature tests.
Setup¶
First, the test code reads the following two values from the cattle_grid configuration file:
amqp_url = "amqp://guest:guest@rabbitmq:5672/"
db_url = "postgresql+asyncpg://postgres:pass@cattle_grid_db/"
The amqp_url is used by almabtrieb. The db_url is currently needed
to create and delete accounts to use almabtrieb with.
Project layout¶
Assuming one locates the feature tests in the features directory,
we will need to create the following structure:
features/
├── basic.feature
├── environment.py
└── steps
├── custom.py
└── import.py
where basic.feature is the Gherkin for some test and
custom.py represents custom steps. The file environment.py
is described here
in the behave documentation and should take the form
from cattle_grid.testing.features.environment import (
before_all,
before_scenario,
after_scenario,
)
similarly import.py makes the steps available:
Custom setup steps¶
If you have to run custom setup steps, you can do it as follows:
from cattle_grid.testing.features.environment import (
before_all,
before_scenario as cg_bs,
after_scenario,
)
def before_scenario(context, scenario):
cg_bs(context, scenario)
# your setup
Usage¶
One thing these tools do for you is managing actors for this to work, one needs to create actors using our methods
this ensures that the context.actors["Alice"] and context.connections["Alice"]
variables have the appropriate values.
cattle_grid.testing.features ¶
fetch_request
async
¶
Sends a fetch request for the uri through the gateway
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
The behave context |
required | |
username
|
str
|
username performing the result |
required |
uri
|
str
|
URI being looked up |
required |
Returns:
| Type | Description |
|---|---|
dict | None
|
|
Source code in cattle_grid/testing/features/__init__.py
cattle_grid.testing.features.environment ¶
The before_all, _scenario, and after_scenario functions need to be imported in your environment.py file, e.g.
from cattle_grid.testing.features.environment import (
before_all, # noqa
before_scenario, # noqa
after_scenario, # noqa
)
from cattle_grid.testing.features.reporting import (
before_step, # noqa
)
after_scenario ¶
Called in features/environment.py
Deletes the created actors and associated accounts. Closes the aiohttp.ClientSession.
Source code in cattle_grid/testing/features/environment.py
before_all ¶
before_all(context: Context)
Called in features/environment.py
Ensures that default variables, context.session, .actors, .connections
exist.
Source code in cattle_grid/testing/features/environment.py
before_scenario ¶
Called in features/environment.py
Opens an aiohttp.ClientSession and sets it to context.session.