Lifecycle Decorators
The Task
class can read a series of lifecycle methods. The structure is inspired by unit tests.
A task can have multiple of the same lifecycle methods. Lifecycle methods are imported directly =
from the cadence
package:
from cadence import after_all, after_each, before_all, before_each, step
step
All step
is considered a subtask within the task. Each step runs an independent section of the task.
Additional metadata not available to the other lifecycle methods is also available to
Parameters
Name | Type | Default | Description |
---|---|---|---|
order | Optional[int] | 1 | The order this step executes in with respect to other task steps. |
name | Optional[str] | None | The human-readable name of the step. |
desc | Optional[str] | None | A brief description about what this step is trying to accomplish. |
Example
import requests
class Test(Task):
joke = None
@step(order=1, name="Get dad joke")
def obtain_jokes(self):
api_url = "https://icanhazdadjoke.com/"
headers = {"Accept": "application/json"}
response = requests.get(api_url, headers=headers)
data = response.json()
assert "joke" in data
self.joke = data["joke"]
@step(order=2, name="Tell dad joke")
def tell_dad_joke(self):
print(self.joke)
@step(order=3, name="Delete dad joke")
def delete_dad_joke(self):
self.joke = None
Steps can also be defined out of order but called within the order defined by order
:
@step(order=2, name="Tell dad joke")
def tell_dad_joke(self):
# runs second
@step(order=3, name="Delete dad joke")
def delete_dad_joke(self):
# runs third
@step(order=1, name="Get dad joke")
def obtain_jokes(self):
# runs first
Steps sharing the same order will be called in the order they are defined
@step(order=1, name="Tell dad joke")
def tell_dad_joke(self):
# runs first
@step(order=3, name="Delete dad joke")
def delete_dad_joke(self):
# runs third
@step(order=1, name="Get dad joke")
def obtain_jokes(self):
# runs second
before_all
All before_all
methods run at the beginning of the test.
Parameters
Name | Type | Default | Description |
---|---|---|---|
fail_silently | Optional[bool] | False | Whether an exception raised by this method fails the test. |
Example
class Test(Task):
drivers = None
@before_all(fail_silently=False)
def test_before_all(self):
# example: configure Selenium web drivers prior to using them
self.drivers = configure_drivers()
before_each
All before_each
methods run before each step.
Parameters
Name | Type | Default | Description |
---|---|---|---|
fail_silently | Optional[bool] | False | Whether an exception raised by this method fails the test. |
Example
class Test(Task):
# ...
@before_each(fail_silently=True)
def test_before_each(self):
# example: delete selenium cookies before each step
self.drivers.delete_all_cookies()
after_all
All after_all
methods run at the end of the test.
Parameters
Name | Type | Default | Description |
---|---|---|---|
fail_silently | Optional[bool] | False | Whether an exception raised by this method fails the test. |
Example
class Test(Task):
drivers = None
@after_all(fail_silently=False)
def test_after_all(self):
# example: configure Selenium web drivers prior to using them
self.drivers = configure_drivers()
after_each
All after_each
methods run after each step.
Parameters
Name | Type | Default | Description |
---|---|---|---|
fail_silently | Optional[bool] | False | Whether an exception raised by this method fails the test. |
Example
class Test(Task):
# ...
@after_each(fail_silently=True)
def test_after_each(self):
# example: delete selenium cookies after each step
self.drivers.delete_all_cookies()