Run Pytest
- Run a specific test file
pytest path/to/test_file.py
- Run a test in it:
pytest path/to/test_file.py::test_function_name
Assert
- For integer assertions:
1
assert (1==1)
- For float assertions:
1
2
import pytest
1.0==pytest.approx(1.0)
- For numpy array assertions:
1
2
3
4
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([1, 2, 3])
np.testing.assert_allclose(array1, array2)
Using VSCode Debugger With Pytest
ctrl+shift+p
choosedebug tests in the current file
ordebug all tests
(if you want to debug all tests under a configured directory)- In my debugger, I found that I have to manually set a breakpoint before the failure point in Pytest. (I might miss an easier route to get around this)
-
At the failed test, right click, and choose debug test
pytest -s <my_test.py>
seems to be executing all test modules in the current directory: this will be enforced in apyproject.toml
environment.
Test Fixture
The boiler plate is:
1
2
3
4
5
6
7
8
9
import pytest
@pytest.fixture
def basic_config():
return {
"batch_size": 2,
}
def test_og_positional_encoder(basic_config):
batch_size = basic_config["batch_size"]
...