2023-11-14 18:05:07 +00:00
# Trading Market-Sim End-To-End Tests
2024-02-22 20:50:55 +00:00
This directory contains end-to-end tests for the Trading application using Vega-market-sim. This guide will help you set up your environment and run the tests efficiently.
2023-11-14 18:05:07 +00:00
## Prerequisites
2024-02-22 20:50:55 +00:00
Ensure you have the following installed:
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
- [Poetry ](https://python-poetry.org/docs/#installing-with-the-official-installer ) for dependency management.
- [Docker ](https://www.docker.com/ ) for running isolated application containers.
- Python, versions ">=3.9,< 3.11 ". Install from the [official Python website ](https://www.python.org/ ).
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
## Setup
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
### 1. Install Dependencies
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
- **Poetry**: Follow the installation guide on the [official Poetry website ](https://python-poetry.org/docs/#installing-with-the-official-installer ).
- **Docker**: Installation instructions are available on the [official Docker website ](https://docs.docker.com/desktop/ ).
- **Python**: Install a version between 3.9 and 3.11, as detailed on the [official Python website ](https://www.python.org/ ).
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
### 2. Configure Your Environment
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
Ensure you're in the tests folder before executing commands.
2023-12-19 12:22:15 +00:00
2023-11-14 18:05:07 +00:00
```bash
2024-02-22 20:50:55 +00:00
poetry shell
poetry update vega-sim # Updates to the latest version of the market-sim branch
2023-11-14 18:05:07 +00:00
poetry install
2024-02-22 20:50:55 +00:00
playwright install chromium # Installs necessary browsers for Playwright
2023-11-14 18:05:07 +00:00
```
2024-02-22 20:50:55 +00:00
### 3. Prepare Binaries and Docker Images
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
Download necessary binaries for the desired Vega version:
2023-11-14 18:05:07 +00:00
```bash
python -m vega_sim.tools.load_binaries --force --version $VEGA_VERSION
```
2024-02-22 20:50:55 +00:00
Pull Docker images for your environment:
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
- **Development**: `docker pull vegaprotocol/trading:develop`
- **Production**: `docker pull vegaprotocol/trading:main`
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
### 4. Build a Docker Image of Your Locally Built Trading App
2023-11-14 18:05:07 +00:00
```bash
2024-02-22 20:50:55 +00:00
./docker/prepare-dist.sh
2023-11-14 18:05:07 +00:00
docker build -f docker/node-outside-docker.Dockerfile --build-arg APP=trading --build-arg ENV_NAME=stagnet1 -t vegaprotocol/trading:latest .
```
2024-02-22 20:50:55 +00:00
## Running Tests
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
Ensure the Docker daemon is running. Update the `.env` file with the correct trading image before proceeding.
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
- **Run all tests**: `poetry run pytest`
- **Run a specific test**: `poetry run pytest -k "test_name" -s --headed`
- **Run tests using your locally served console**:
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
In one terminal window, build and serve the trading console:
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
```bash
yarn nx build trading
yarn nx serve trading
```
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
Once the console is served, update the `.env` file to set `local_server=true` . You can then run your tests using the same commands as above.
NOTE: Parallel running of tests will not work against locally served console.
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
## Test Strategy and Container Cleanup
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
### Strategy
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
We aim for each test file to use a single Vega instance to ensure test isolation and manage resources efficiently. This approach helps in maintaining test performance and reliability.
2023-12-13 12:46:16 +00:00
2024-02-22 20:50:55 +00:00
### Cleanup Procedure
2023-12-13 12:46:16 +00:00
2024-02-22 20:50:55 +00:00
To ensure proper cleanup of containers after each test, use the following fixture pattern:
2023-12-13 12:46:16 +00:00
2024-02-22 20:50:55 +00:00
```python
@pytest .fixture
def vega(request):
with init_vega(request) as vega_instance:
request.addfinalizer(lambda: cleanup_container(vega_instance))
yield vega_instance
2023-11-14 18:05:07 +00:00
```
2024-02-22 20:50:55 +00:00
## Running Tests in Parallel
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
For running tests in parallel:
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
- **Within the e2e folder**: `poetry run pytest -s --numprocesses auto --dist loadfile`
- **From anywhere**: `yarn trading:test:all`
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
## Troubleshooting
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
If IntelliSense is not working in VSCode, follow these steps:
2023-11-14 18:05:07 +00:00
2024-02-22 20:50:55 +00:00
1. Find the Poetry environment's Python binary: `poetry run which python`
2. In VSCode, open the command menu (`cmd + shift + p`), search for `Python: Select Interpreter` , select `Enter interpreter path` , and paste the path from step 1.