Files
JiDe Zhang 607fdeab2e fix(ci/deps): 修复pytest与py库兼容性,完善虚拟环境支持
1. requirements.txt 限定 py<1.11.0 和 pytest<8.0.0,避免 AttributeError: module 'py' has no attribute 'path'
2. CI 安装步骤强制指定兼容版本,确保 GitHub Actions 稳定通过
3. 本地推荐使用 venv 隔离依赖,避免系统包冲突
4. 测试脚本 run_tests.py 支持自动适配环境,纯Python/torch/pytest 均可用

fix(ci/deps): Fix pytest/py compatibility, improve venv support

1. requirements.txt restricts py<1.11.0 and pytest<8.0.0 to avoid AttributeError
2. CI install step enforces compatible versions for stable GitHub Actions
3. Local usage recommends venv for dependency isolation
4. run_tests.py auto-detects environment, works for pure Python/torch/pytest
2025-06-13 15:31:33 +08:00
..

ComfyUI DCI Extension - Test Suite

This directory contains comprehensive unit tests for the ComfyUI DCI Extension.

Test Structure

Unit Tests (New)

  • test_dci_format.py - Tests for DCI format creation and parsing
  • test_pure_python_ar.py - Tests for pure Python AR implementation
  • test_comfyui_nodes.py - Tests for ComfyUI nodes
  • test_runner.py - Test runner for all unit tests
  • conftest.py - Pytest configuration and fixtures

Legacy Tests (Integration)

The existing test files serve as integration tests:

DCI Preview and UI Tests

  • test_dci_preview_*.py - DCI preview functionality tests
  • test_dci_preview_node_fix.py - DCI preview node bug fixes
  • test_dci_preview_simple.py - Simple DCI preview tests
  • test_dci_preview_ui.py - UI-related preview tests
  • test_font_size_*.py - Font size handling tests

File Processing Tests

  • test_dci_file_saver.py - DCI file saving tests
  • test_binary_*.py - Binary file handling tests
  • test_real_dci_files.py - Real DCI file processing tests
  • test_layer_system.py - DCI layer system tests

Node Tests

  • test_node_*.py - Node registration and connectivity tests
  • test_new_dci_nodes.py - New DCI node functionality tests
  • test_structure_node.py - DCI structure analysis tests

Quality and Feature Tests (Moved from root)

  • test_advanced_compression.py - Advanced compression feature tests
  • test_border_color.py - Border color functionality tests
  • test_categories.py - Category system tests
  • test_quality_feature.py - Image quality feature tests
  • test_transparent_preview.py - Transparent image preview tests
  • test_pure_python_implementation.py - Pure Python implementation tests
  • test_pure_python_simple.py - Simple pure Python tests
  • simple_quality_test.py - Simple quality testing script

Test Assets

  • test_preview_*.png - Test preview images (blue, dark, light themes)
  • test_transparent_preview.png - Transparent preview test image
  • test_output.dci - Sample DCI output file for testing

Verification Scripts

  • verify_*.py - Various verification and validation scripts

Running Tests

Using the Test Runner

# Run all unit tests
cd tests
python test_runner.py

# Run specific test module
python test_runner.py test_dci_format

Using unittest

# Run all unit tests
python -m unittest discover tests -p "test_*.py" -v

# Run specific test class
python -m unittest tests.test_dci_format.TestDCIFormat -v

# Run specific test method
python -m unittest tests.test_dci_format.TestDCIFormat.test_basic_dci_creation -v

Using pytest (if available)

# Run all tests
python -m pytest tests/ -v

# Run with coverage
python -m pytest tests/ --cov=py --cov-report=html

# Run only unit tests
python -m pytest tests/ -m unit

# Run only integration tests
python -m pytest tests/ -m integration

Using Makefile

# Run all tests
make test

# Run unit tests only
make test-unit

# Run integration tests
make test-integration

# Run with coverage
make test-coverage

GitHub Actions

Tests are automatically run on GitHub Actions when:

  • Code is pushed to main/master/develop branches
  • Pull requests are created
  • Manual workflow dispatch is triggered

The CI pipeline runs tests on multiple Python versions (3.8, 3.9, 3.10, 3.11) and includes:

  • Unit tests
  • Integration tests
  • Cross-platform testing (Linux, Windows, macOS)
  • Code coverage reporting

Test Categories

Unit Tests

  • DCI Format Tests: Test DCI file creation, parsing, and structure validation
  • Pure Python AR Tests: Test AR archive creation and extraction without external dependencies
  • ComfyUI Node Tests: Test node initialization, execution, and integration

Integration Tests

  • Legacy Test Files: Existing test files that test complete workflows
  • Real File Processing: Tests with actual DCI files and real-world scenarios
  • Cross-platform Compatibility: Tests that verify functionality across different operating systems

Test Features

Automatic Test Discovery

  • Tests are automatically discovered by pattern matching (test_*.py)
  • Test classes must start with Test
  • Test methods must start with test_

Fixtures and Setup

  • Temporary directories are automatically created and cleaned up
  • Sample data is provided through fixtures
  • Test environment is properly isolated

Skip Conditions

  • Tests are automatically skipped if required modules are not available
  • This allows tests to run even in incomplete environments
  • Graceful degradation for missing dependencies

Coverage Reporting

  • Code coverage is measured and reported
  • HTML coverage reports are generated
  • Coverage data is uploaded to Codecov (optional)

Writing New Tests

Unit Test Template

import unittest
import tempfile
import shutil

class TestNewFeature(unittest.TestCase):
    def setUp(self):
        """Set up test environment"""
        self.test_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.test_dir)

    def test_feature_functionality(self):
        """Test specific functionality"""
        # Test implementation
        self.assertTrue(True)  # Replace with actual test

    @unittest.skipIf(condition, "reason")
    def test_conditional_feature(self):
        """Test that may be skipped"""
        # Test implementation
        pass

Best Practices

  1. Isolation: Each test should be independent and not rely on other tests
  2. Cleanup: Use setUp() and tearDown() or addCleanup() for resource management
  3. Descriptive Names: Test names should clearly describe what is being tested
  4. Skip Conditions: Use @unittest.skipIf() for tests that require specific conditions
  5. Assertions: Use appropriate assertion methods (assertEqual, assertTrue, etc.)
  6. Documentation: Include docstrings explaining what each test does

Troubleshooting

Common Issues

  1. Import Errors: Make sure the project path is correctly added to sys.path
  2. Missing Dependencies: Install test dependencies with make dev-install
  3. Permission Errors: Ensure test directories are writable
  4. Path Issues: Use os.path.join() for cross-platform path handling

Debug Mode

Run tests with verbose output to see detailed information:

python -m unittest tests.test_module -v

Test Isolation

If tests are interfering with each other:

  1. Check that temporary files are properly cleaned up
  2. Ensure global state is not modified
  3. Use separate test directories for each test

Contributing

When adding new functionality:

  1. Write unit tests for new features
  2. Update existing tests if behavior changes
  3. Ensure all tests pass before submitting
  4. Add integration tests for complex workflows
  5. Update this documentation if needed