pytest 测试运行
run-tests
本地运行 pytest 测试的指南。在运行测试以验证代码更改时使用。
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 600
- Forks
- 204
01 前言 Front Matter
- name
- run-tests
- description
- Guide for running pytest tests locally. Use this when running tests to verify code changes.
02 资源文件 6
- 参考文档references/README_gh_code_search.md3.6 KB
- 参考文档references/README_gh_failed_run.md3.9 KB
- 参考文档references/README_pages.md7.4 KB
- 脚本scripts/gh_code_search.py10.0 KB
- 脚本scripts/gh_failed_run.py8.3 KB
- 脚本scripts/gh_pages_deploy.py13.4 KB
03 SKILL.md 正文
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
# Install package in editable mode with test 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
# Run all tests
pytest -s tests/
# Run with verbose output
pytest -sv tests/
# Run specific test file
pytest -s tests/test_example.py
# Run specific test by name pattern
pytest -s tests/ -k test_my_function
# Run multiple tests by pattern
pytest -s tests/ -k "test_list or test_create"
# Stop on first failure
pytest -sx tests/
Test Discovery
pytest automatically discovers tests in:
- Files named
test_*.pyor*_test.py - Functions named
test_* - Classes named
Test*
Test Output Options
Verbose Output
# Show test names and results
pytest -v tests/
# Show test names with print statements
pytest -sv tests/
Show Print Statements
The -s flag captures stdout:
# Show print output during tests
pytest -s tests/
Show Test Durations
# Show slowest 10 tests
pytest --durations=10 tests/
Filtering Tests
By Test Name
# Run tests matching pattern
pytest -k "workspace" tests/
# Run tests NOT matching pattern
pytest -k "not slow" tests/
# Combine patterns
pytest -k "workspace and not admin" tests/
By File
# Run specific test file
pytest tests/test_workspaces.py
# Run tests in directory
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:
# Run all tests
pytest -s tests/
# Run tests for modified area
pytest -s tests/ -k relevant_test_pattern
# Check for any failures
# If failures, fix code and re-run
Common Test Issues
Import Errors
If tests fail with import errors:
# Ensure package is installed in editable mode
pip install -e .
Missing Dependencies
If tests fail with missing package:
# Install test dependencies
pip install -e ".[test]"
Environment Issues
If tests behave unexpectedly:
# Recreate conda environment
conda env remove -n fabric
conda env create -f environment.yml
conda activate fabric
pip install -e .
04 同仓库更多 Skills
Python 单元测试编写
write-tests
编写单元测试指南。在创建测试以验证 Python 逻辑时使用。
UI 视觉样式设计
ui-styling
关于 Semantic Link Labs 交互式 UI 工具(HTML 小部件和基于 anywidget 的小部件)所使用的视觉风格、结构和共享构建块的指南。在添加新的交互式 UI、修改现有 UI 或添加共享视觉组件时使用此技能。
TOM 表格对象模型操作
tom-operations
关于使用 TOM(表格对象模型,Tabular Object Model)包装器的指南。当以编程方式修改语义模型时使用此技能。
SQL 转 DAX 度量值
sql_to_dax
将 SQL 聚合表达式转换为 DAX 度量值的指南。
REST API 封装实现
rest-api-patterns
实现 REST API 包装函数的指南。在添加新的 API 包装器或排查 API 调用问题时使用。
DAX 查询构建器指南
query-builder-dax
关于 DAX 测试小部件中查询构建器生成的 DAX 查询形状的指南。当需要修改查询构建器生成 DAX 的方式,或从列、度量值、筛选器和排序生成 EVALUATE 查询时使用。