play-python

pytest-play plugin with python expressions and assertions


Keywords
play_python, pytest, python, testing
License
Apache-2.0
Install
pip install play-python==0.1.1

Documentation

play python

Documentation Status

pytest-play plugin with restricted Python expressions and assertions and it is based on the RestrictedPython package.

RestrictedPython is a tool that helps to define a subset of the Python language which allows to provide a program input into a trusted environment. RestrictedPython is not a sandbox system or a secured environment, but it helps to define a trusted environment and execute untrusted code inside of it.

See:

More info and examples on:

Features

This project defines the following pytest-play commands based on Python expressions.

Store variables

You can store a pytest-play variables:

{
 'provider': 'python',
 'type': 'store_variable',
 'expression': '1+1',
 'name': 'foo'
}

Make a Python assertion

You can make an assertion based on a Python expression:

{
 'provider': 'python',
 'type': 'assert',
 'expression': 'variables["foo"] == 2'
}

Sleep

Sleep for a given amount of seconds:

{
 'provider': 'python',
 'type': 'sleep',
 'seconds': 2
}

Exec a Python expresssion

You can execute a Python expression:

{
 'provider': 'python',
 'type': 'exec',
 'expression': 'variables.update({'play_requests': {'parameters': {'headers': {'Authorization': '$bearer', 'Content-Type': 'application/json'}}}})'
}

Wait until condition

The wait_until_not command waits until the wait expression is False:

{
 'provider': 'python',
 'type': 'wait_until_not',
 'expression': 'variables["expected_id"] is not None and variables["expected_id"][0] == $id',
 'timeout': 5,
 'poll': 0.1,
 'subcommands': [{
     'provider': 'play_sql',
     'type': 'sql',
     'database_url': 'postgresql://$db_user:$db_pwd@$db_host/$db_name',
     'query': 'SELECT id FROM table WHERE id=$id ORDER BY id DESC;',
     'variable': 'expected_id',
     'expression': 'results.first()'
 }]
}

assuming that the subcommand updates the execution results updating a pytest-play variable (eg: expected_id) where tipically the $id value comes from a previously executed command that causes an asynchrounous update on a relational database soon or later (eg: a play_requests command making a HTTP POST call or a MQTT message coming from a simulated IoT device with play_mqtt).

The wait command will try (and retry) to execute the subcommand with a poll frequency poll (default: 0.1 seconds) until the provided timeout expressed in seconds expires or an exception occurs.

You can use the opposite command named wait_until that waits until the wait expression is not False.

Loop commands

You can repeat a group of subcommands using a variable as a counter. Assuming you have defined a countdown variable with 10 value, the wait until command will repeat the group of commands for 10 times:

play_json.execute_command({
    'provider': 'python',
    'type': 'wait_until',
    'expression': 'variables["countdown"] == 0',
    'timeout': 0,
    'poll': 0,
    'sub_commands': [{
        'provider': 'python',
        'type': 'store_variable',
        'name': 'countdown',
        'expression': 'variables["countdown"] - 1'
    }]
})

Twitter

pytest-play tweets happens here:

Credits

This package was created with Cookiecutter and the cookiecutter-play-plugin (based on audreyr/cookiecutter-pypackage project template).