Set up your practice project
Why This Matters
From the next lesson on, you test a real machine learning system: you train a model, prepare data, calculate metrics, and hunt for bias and broken metamorphic relations. All of that happens in one small Python project that grows with the course. Twenty minutes of setup now means every later exercise starts with writing a test, not with fighting an installation.
What you will learn
- Install Python and create an isolated project environment
- Install the packages the exercises use
- Run the setup tests that prove your project can reach the practice system
What you will test
The exercises test the loan approval model on practiceautomatedtesting.com. It is a practice system built for this course: a model that decides whether an applicant gets a loan, its training and production data, and a labelled test set. It has known defects on purpose. You reach it through the ML practice API, so every exercise is a test you can run again.
Two ways to work
The example repository learn-automated-testing/ai-testing-practice has the solution of every exercise. Choose one way:
- Build it yourself (recommended): start with an empty folder and write each file as the lessons describe. You learn more from the code you type. Look at the repository when you are stuck.
- Clone it: start from the finished repository. Useful when you start halfway through the course, or when you only want to run and read the tests.
The setup below is the same for both, except step 2.
1. Install Python
You need Python 3.10 or newer. Open a terminal and check:
python3 --version
On Windows, use py --version. If Python is missing or older than 3.10, install the current version from python.org/downloads. On Windows, tick Add python.exe to PATH on the first screen of the installer.
2. Create the project
Build it yourself:
mkdir ai-testing-practice
cd ai-testing-practice
Or clone it:
git clone https://github.com/learn-automated-testing/ai-testing-practice.git
cd ai-testing-practice
3. Create a virtual environment
python3 -m venv .venv
A virtual environment is a private copy of Python in the folder .venv. Packages you install for this course go there, not into the Python of your computer, so they cannot break other projects and other projects cannot break them.
Activate it. You do this every time you open a new terminal for the project:
source .venv/bin/activate # macOS and Linux
.venv\Scripts\activate # Windows
Your prompt now starts with (.venv). If you forget to activate, pytest is “not found” or uses packages from somewhere else; activating is the first thing to check when something behaves strangely.
4. Install the packages
If you cloned the repository, the list is already there:
pip install -r requirements.txt
If you build it yourself, create requirements.txt first:
requests>=2.32,<3
pytest>=8,<10
pandas>=2.2,<4
scikit-learn>=1.5,<2
scipy>=1.13,<2
numpy>=2,<3
and then run the same pip install -r requirements.txt.
| Package | Used for |
|---|---|
| requests | Calling the ML practice API |
| pytest | Finding and running your tests, and explaining failures |
| pandas | Working with datasets as tables (input data testing, module 6) |
| scikit-learn | Training and evaluating models (this module and module 4) |
| scipy, numpy | Statistical tests and calculations (modules 5 to 7) |
A notice like A new release of pip is available is harmless. You can update pip with pip install --upgrade pip, or ignore it.
5. Add the shared settings and the setup tests
If you cloned the repository, these files are already there; read them and go to step 6.
Create pytest.ini, which tells pytest where the tests are:
[pytest]
testpaths = setup module-3 module-4 module-5 module-6 module-7
addopts = -ra
Create conftest.py. pytest loads this file automatically; the api fixture in it gives every test the address of the API, in one place:
import os
import pytest
import requests
ML_API_BASE = os.environ.get("ML_API_BASE", "https://practiceautomatedtesting.com/api/ml").rstrip("/")
@pytest.fixture(scope="session")
def api():
"""A requests session for the ML practice API, with the base URL in `api.base`."""
session = requests.Session()
session.base = ML_API_BASE
yield session
session.close()
Create the folder setup with the file setup/test_setup.py:
"""If these tests pass, your project is ready."""
import sys
def test_python_is_recent_enough():
assert sys.version_info >= (3, 10), f"Python 3.10 or newer is needed, this is {sys.version.split()[0]}"
def test_the_packages_are_installed():
import pandas # noqa: F401
import requests # noqa: F401
import sklearn # noqa: F401
def test_the_ml_practice_api_is_reachable(api):
response = api.get(f"{api.base}/health", timeout=10)
assert response.status_code == 200
assert response.json()["models"] == ["v1", "v2"]
Finally, a .gitignore, so the virtual environment never ends up in Git:
.venv/
__pycache__/
.pytest_cache/
6. Run the setup tests
pytest setup -v
setup/test_setup.py::test_python_is_recent_enough PASSED [ 33%]
setup/test_setup.py::test_the_packages_are_installed PASSED [ 66%]
setup/test_setup.py::test_the_ml_practice_api_is_reachable PASSED [100%]
Three green tests: Python is recent enough, the packages are installed, and your computer can reach the practice system. Your project is ready for every exercise in the course.
If you cloned the repository, pytest -v runs the finished exercises too. This is a real run against the live site:
platform darwin -- Python 3.14.0, pytest-9.2.0, pluggy-1.7.0
configfile: pytest.ini
testpaths: setup, module-3, module-4, module-5, module-6, module-7
collected 7 items
setup/test_setup.py::test_python_is_recent_enough PASSED [ 14%]
setup/test_setup.py::test_the_packages_are_installed PASSED [ 28%]
setup/test_setup.py::test_the_ml_practice_api_is_reachable PASSED [ 42%]
module-4/test_metrics.py::test_my_confusion_matrix_matches_the_api[0.3] PASSED [ 57%]
module-4/test_metrics.py::test_my_confusion_matrix_matches_the_api[0.5] PASSED [ 71%]
module-4/test_metrics.py::test_my_confusion_matrix_matches_the_api[0.8] PASSED [ 85%]
module-4/test_metrics.py::test_precision_and_recall_meet_the_acceptance_criteria PASSED [100%]
============================== 7 passed in 19.31s ==============================
When something fails
| You see | Cause and fix |
|---|---|
python3: command not found | Python is not installed or not on the PATH. Install it from python.org; on Windows use py instead of python3. |
Windows: running scripts is disabled on this system when activating | PowerShell blocks scripts by default. Run once: Set-ExecutionPolicy -Scope CurrentUser RemoteSigned, then activate again. |
pytest: command not found | The virtual environment is not active. Run the activate command from step 3. |
test_the_packages_are_installed fails with ModuleNotFoundError | The packages went into another Python. Activate the environment, then pip install -r requirements.txt again. |
test_the_ml_practice_api_is_reachable fails with a connection error | Your network blocks the site (a company proxy, for example). Open https://practiceautomatedtesting.com/api/ml/health in a browser; if that works there but not in Python, ask for the proxy settings and set HTTPS_PROXY. |
Your project so far
ai-testing-practice/
├── .venv/ the virtual environment (not in Git)
├── .gitignore
├── conftest.py the shared `api` fixture
├── pytest.ini where the tests are
├── requirements.txt the packages
└── setup/
└── test_setup.py the setup checks
Each hands-on lesson adds one file to this project, in the folder of its module, and ends with this overview.