Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parallel testing section to readme #320

Merged
merged 4 commits into from
May 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,37 +200,61 @@ Run tests using [tox](https://tox.wiki/en/latest/), tox automatically creates an
tox

====================== test session starts ======================
platform linux -- Python 3.7.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/readme/cairo-contracts
plugins: asyncio-0.16.0, web3-5.24.0, typeguard-2.13.0
collected 19 items

tests/test_Account.py .... [ 21%]
tests/test_AddressRegistry.py .. [ 31%]
tests/test_ERC20.py .......... [ 84%]
tests/test_Initializable.py . [ 89%]
tests/test_Ownable.py .. [100%]
platform linux -- Python 3.7.2, pytest-7.1.2, py-1.11.0, pluggy-1.0.0
rootdir: /home/readme/cairo-contracts, configfile: tox.ini
plugins: asyncio-0.18.3, xdist-2.5.0, forked-1.4.0, web3-5.29.0, typeguard-2.13.3
asyncio: mode=auto
gw0 [185] / gw1 [185]
........................................................................................................................................................................................ [100%]
```

### Run Tests in Docker

For M1 users or those who are having trouble with library/python versions you can alternatively run the tests within a docker container. Using the following as a Dockerfile placed in the root directory of the project:
```

```dockerfile
FROM python:3.7

RUN pip install tox
RUN mkdir cairo-contracts
COPY . cairo-contracts
WORKDIR cairo-contracts
ENTRYPOINT tox

```

After its placed there run:
```

```bash
docker build -t cairo-tests .
docker run cairo-tests
```

### Parallel Testing

This repo utilizes the [pytest-xdist](https://pytest-xdist.readthedocs.io/en/latest/) plugin which runs tests in parallel. This feature increases testing speed; however, conflicts with a shared state can occur since tests do not run in order. To overcome this, independent cached versions of contracts being tested should be provisioned to each test case. Here's a simple fixture example:

```python
from utils import get_contract_def, cached_contract

@pytest.fixture(scope='module')
def foo_factory():
# get contract definition
foo_def = get_contract_def('path/to/foo.cairo')

# deploy contract
starknet = await Starknet.empty()
foo = await starknet.deploy(contract_def=foo_def)

# copy the state and cache contract
state = starknet.state.copy()
cached_foo = cached_contract(state, foo_def, foo)

return cached_foo
```

See [Memoization](docs/Utilities.md#memoization) in the Utilities documentation for a more thorough example on caching contracts.

> Note that this does not apply for stateless libraries such as SafeMath.

## Security

Expand Down