| name | run-tests |
| description | Guide for running pytest tests locally. Use this when running tests to verify code changes. |
Running Tests
This skill covers running pytest tests for the Semantic Link Labs project.
When to Use This Skill
Use this skill when you need to:
- Run unit tests to verify code changes
- Run specific tests by name
- Debug failing tests
- Validate changes before committing
Test Framework
| Component | Details |
|---|
| Framework | pytest |
| Location | tests/ directory |
| Configuration | pyproject.toml |
Prerequisites
Install Development Dependencies
pip install -e ".[test]"
Environment Setup
Create conda environment from environment.yml:
conda env create -f environment.yml
conda activate fabric
pip install -e .
Running Tests
Basic Commands
pytest -s tests/
pytest -sv tests/
pytest -s tests/test_example.py
pytest -s tests/ -k test_my_function
pytest -s tests/ -k "test_list or test_create"
pytest -sx tests/
Test Discovery
pytest automatically discovers tests in:
- Files named
test_*.py or *_test.py
- Functions named
test_*
- Classes named
Test*
Test Output Options
Verbose Output
pytest -v tests/
pytest -sv tests/
Show Print Statements
The -s flag captures stdout:
pytest -s tests/
Show Test Durations
pytest --durations=10 tests/
Filtering Tests
By Test Name
pytest -k "workspace" tests/
pytest -k "not slow" tests/
pytest -k "workspace and not admin" tests/
By File
pytest tests/test_workspaces.py
pytest tests/admin/
Debugging Failed Tests
Stop on First Failure
pytest -x tests/
Enter Debugger on Failure
pytest --pdb tests/
Show Local Variables on Failure
pytest -l tests/
Increase Verbosity
pytest -vvv tests/
Test Structure
Basic Test Example
import pytest
import pandas as pd
def test_my_function_returns_dataframe():
"""Test that my_function returns a DataFrame."""
from sempy_labs import my_function
result = my_function()
assert isinstance(result, pd.DataFrame)
def test_my_function_with_parameter():
"""Test my_function with specific parameter."""
from sempy_labs import my_function
result = my_function(workspace="Test Workspace")
assert not result.empty
assert "Name" in result.columns
Test with Expected Exception
def test_my_function_raises_on_invalid_input():
"""Test that my_function raises ValueError on invalid input."""
from sempy_labs import my_function
with pytest.raises(ValueError, match="Invalid"):
my_function(invalid_param="bad value")
CI/CD Integration
The project uses GitHub Actions for CI. See .github/workflows/build.yaml:
- name: Test with pytest
shell: bash -el {0}
run: |
pytest -s tests/
Pre-Commit Test Checklist
Before committing code changes:
pytest -s tests/
pytest -s tests/ -k relevant_test_pattern
Common Test Issues
Import Errors
If tests fail with import errors:
pip install -e .
Missing Dependencies
If tests fail with missing package:
pip install -e ".[test]"
Environment Issues
If tests behave unexpectedly:
conda env remove -n fabric
conda env create -f environment.yml
conda activate fabric
pip install -e .