Skip to content

test: standardize tests from unittest to pytest - #41

Merged
adiadd merged 3 commits into
devfrom
fix/standardize-tests
Dec 3, 2025
Merged

adiadd merged 3 commits into
devfrom
fix/standardize-tests

Conversation

@adiadd

@adiadd adiadd commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

Issue #, if available:

N/A

Description of changes:

Standardized all test files from unittest to pytest framework for improved test functionality and modern Python testing practices.

Changes include:

  • Added pytest configuration in pyproject.toml with test discovery settings
    and default options for verbose output and short tracebacks
  • Converted test classes from unittest.TestCase inheritance to plain classes
  • Replaced setUp() methods with pytest's setup_method()
  • Replaced unittest assertions (assertEqual, assertAlmostEqual, etc.) with
    native Python assert statements
  • Used pytest.approx() for floating point comparisons instead of assertAlmostEqual()

All tests pass as before:

=== 741 passed, 16 skipped, 95 warnings in 51.78s ===

Potential Future Enhancements:

  1. Add pytest-mock:
      # pyproject.toml
   dev = [
       "pytest>=7.0.0",
       "pytest-xdist>=3.0.0",
       "pytest-mock>=3.0.0",  # Add this
       ...
   ]
  Benefit: Cleaner fixture-based mocking vs manual patcher lifecycle
  1. Parametrize Simple Comparator Tests:
    • test_exact.py and test_numeric.py could use @pytest.mark.parametrize
    • Current approach is also valid - explicit test methods are clear
  2. Utilize Shared Fixtures:
    • Some tests could use a commonconftest.py fixtures instead of inline comparator creation
    • Current inline approach is clear and maintainable
  3. Create documentation/standardization on best practices in testing and create a plan/implement fixes to ensure stickler is adhering to best practices

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copilot AI review requested due to automatic review settings December 3, 2025 05:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR standardizes test files from the unittest framework to pytest, modernizing the testing infrastructure for improved functionality and alignment with contemporary Python testing practices. The conversion affects 35+ test files across the repository, touching structured object evaluator tests, common utility tests, and comparator tests.

Key Changes:

  • Converted all test classes from unittest.TestCase inheritance to plain pytest classes
  • Replaced unittest assertion methods with native Python assert statements
  • Migrated setUp() lifecycle methods to pytest's setup_method()
  • Added pytest configuration in pyproject.toml with test discovery and default options

Reviewed changes

Copilot reviewed 39 out of 39 changed files in this pull request and generated 18 comments.

Show a summary per file
File Description
pyproject.toml Added pytest configuration section with test paths and options
tests/structured_object_evaluator/*.py (30 files) Converted from unittest to pytest patterns: class inheritance, assertions, setup methods
tests/common/*.py (5 files) Migrated utility and evaluator tests to pytest framework
tests/common/comparators/*.py (7 files) Converted comparator tests with proper mock handling
tests/common/algorithms/test_hungarian.py Migrated algorithm tests to pytest with proper approx usage

The migration appears comprehensive and well-executed. All test files follow consistent pytest patterns, with proper conversion of assertions, lifecycle methods, and decorators. The removal of if __name__ == "__main__": unittest.main() blocks is appropriate for pytest-based test discovery.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

score = self.comparator.compare("Hello", "hello")
if score < 1.0:
self.assertLess(score, 1.0) # Should be case sensitive
assert score < 1.0 # Should be case sensitive

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is always true, because of this condition.

Suggested change
assert score < 1.0 # Should be case sensitive
# Should be case sensitive

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on this comment

"""Tests for the anls_score utility functions."""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Suggested change
import pytest

Copilot uses AI. Check for mistakes.
"""Unit tests for the BaseComparator class."""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Suggested change
import pytest

Copilot uses AI. Check for mistakes.
"""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Suggested change
import pytest

Copilot uses AI. Check for mistakes.
"""Tests for edge cases in the structured object evaluator."""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Suggested change
import pytest

Copilot uses AI. Check for mistakes.
"""Test integration of structured models with the main anls_score function."""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Copilot uses AI. Check for mistakes.
"""Tests for structured model comparison using the new StructuredModel implementation."""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Copilot uses AI. Check for mistakes.
"""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Copilot uses AI. Check for mistakes.
import unittest
import pytest
from typing import List
from unittest.mock import patch

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'patch' is not used.

Copilot uses AI. Check for mistakes.
"""

import unittest
import pytest

Copilot AI Dec 3, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Copilot uses AI. Check for mistakes.
@github-actions

github-actions Bot commented Dec 3, 2025

Copy link
Copy Markdown

🔒 Security Scan Results

ASH Security Scan Report

  • Report generated: 2025-12-03T05:45:40+00:00
  • Time since scan: 2 minutes

Scan Metadata

  • Project: ASH
  • Scan executed: 2025-12-03T05:43:39+00:00
  • ASH version: 3.1.2

Summary

Scanner Results

The table below shows findings by scanner, with status based on severity thresholds and dependencies:

  • Severity levels:
    • Suppressed (S): Findings that have been explicitly suppressed and don't affect scanner status
    • Critical (C): Highest severity findings that require immediate attention
    • High (H): Serious findings that should be addressed soon
    • Medium (M): Moderate risk findings
    • Low (L): Lower risk findings
    • Info (I): Informational findings with minimal risk
  • Duration (Time): Time taken by the scanner to complete its execution
  • Actionable: Number of findings at or above the threshold severity level that require attention
  • Result:
    • PASSED = No findings at or above threshold
    • FAILED = Findings at or above threshold
    • MISSING = Required dependencies not available
    • SKIPPED = Scanner explicitly disabled
    • ERROR = Scanner execution error
  • Threshold: The minimum severity level that will cause a scanner to fail
    • Thresholds: ALL, LOW, MEDIUM, HIGH, CRITICAL
    • Source: Values in parentheses indicate where the threshold is set:
      • global (global_settings section in the ASH_CONFIG used)
      • config (scanner config section in the ASH_CONFIG used)
      • scanner (default configuration in the plugin, if explicitly set)
  • Statistics calculation:
    • All statistics are calculated from the final aggregated SARIF report
    • Suppressed findings are counted separately and do not contribute to actionable findings
    • Scanner status is determined by comparing actionable findings to the threshold
Scanner Suppressed Critical High Medium Low Info Actionable Result Threshold
bandit 0 0 0 0 2820 0 0 PASSED MEDIUM (global)
cdk-nag 0 0 0 0 0 0 0 PASSED MEDIUM (global)
cfn-nag 0 0 0 0 0 0 0 MISSING MEDIUM (global)
checkov 0 1 0 0 0 0 1 SKIPPED MEDIUM (global)
detect-secrets 0 0 0 0 0 0 0 PASSED MEDIUM (global)
grype 0 0 0 0 0 0 0 MISSING MEDIUM (global)
npm-audit 0 0 0 0 0 0 0 PASSED MEDIUM (global)
opengrep 0 0 0 0 0 0 0 MISSING MEDIUM (global)
semgrep 0 1 0 0 0 0 1 FAILED MEDIUM (global)
syft 0 0 0 0 0 0 0 MISSING MEDIUM (global)

Top 2 Hotspots

Files with the highest number of security findings:

Finding Count File Location
1 .github/workflows/workflow.yml
1 src/stickler/reporting/html/interactive/main.js

Detailed Findings

Show 2 actionable findings

Finding 1: CKV2_GHA_1

  • Severity: HIGH
  • Scanner: checkov
  • Rule ID: CKV2_GHA_1
  • Location: .github/workflows/workflow.yml:11-12

Description:
Ensure top-level permissions are not set to write-all


Finding 2: javascript.browser.security.insecure-document-method.insecure-document-method

  • Severity: HIGH
  • Scanner: semgrep
  • Rule ID: javascript.browser.security.insecure-document-method.insecure-document-method
  • Location: src/stickler/reporting/html/interactive/main.js:407-414

Description:
User controlled data in methods like innerHTML, outerHTML or document.write is an anti-pattern that can lead to XSS vulnerabilities

Code Snippet:

pdfItem.innerHTML = `
                <div class="pdf-container">
                    <canvas id="pdf-canvas-${escapeHtml(docId)}" class="pdf-canvas"></canvas>
                    <div class="pdf-loading" id="pdf-loading-${escpeHtml(docId)}">Loading PDF...</div>
                    <div class="pdf-error" id="pdf-error-${escapeHtml(docId)}" style="display: none;">Error loading PDF</div>
                </div>
                <p><strong>${escapeHtml(docId)}</strong></p>
            `;

Report generated by Automated Security Helper (ASH) at 2025-12-03T05:45:41+00:00

@github-actions

github-actions Bot commented Dec 3, 2025

Copy link
Copy Markdown

🔒 Security Scan Results

ASH Security Scan Report

  • Report generated: 2025-12-03T05:59:28+00:00
  • Time since scan: 1 minute

Scan Metadata

  • Project: ASH
  • Scan executed: 2025-12-03T05:57:29+00:00
  • ASH version: 3.1.2

Summary

Scanner Results

The table below shows findings by scanner, with status based on severity thresholds and dependencies:

  • Severity levels:
    • Suppressed (S): Findings that have been explicitly suppressed and don't affect scanner status
    • Critical (C): Highest severity findings that require immediate attention
    • High (H): Serious findings that should be addressed soon
    • Medium (M): Moderate risk findings
    • Low (L): Lower risk findings
    • Info (I): Informational findings with minimal risk
  • Duration (Time): Time taken by the scanner to complete its execution
  • Actionable: Number of findings at or above the threshold severity level that require attention
  • Result:
    • PASSED = No findings at or above threshold
    • FAILED = Findings at or above threshold
    • MISSING = Required dependencies not available
    • SKIPPED = Scanner explicitly disabled
    • ERROR = Scanner execution error
  • Threshold: The minimum severity level that will cause a scanner to fail
    • Thresholds: ALL, LOW, MEDIUM, HIGH, CRITICAL
    • Source: Values in parentheses indicate where the threshold is set:
      • global (global_settings section in the ASH_CONFIG used)
      • config (scanner config section in the ASH_CONFIG used)
      • scanner (default configuration in the plugin, if explicitly set)
  • Statistics calculation:
    • All statistics are calculated from the final aggregated SARIF report
    • Suppressed findings are counted separately and do not contribute to actionable findings
    • Scanner status is determined by comparing actionable findings to the threshold
Scanner Suppressed Critical High Medium Low Info Actionable Result Threshold
bandit 0 0 0 0 2820 0 0 PASSED MEDIUM (global)
cdk-nag 0 0 0 0 0 0 0 PASSED MEDIUM (global)
cfn-nag 0 0 0 0 0 0 0 MISSING MEDIUM (global)
checkov 0 1 0 0 0 0 1 SKIPPED MEDIUM (global)
detect-secrets 0 0 0 0 0 0 0 PASSED MEDIUM (global)
grype 0 0 0 0 0 0 0 MISSING MEDIUM (global)
npm-audit 0 0 0 0 0 0 0 PASSED MEDIUM (global)
opengrep 0 0 0 0 0 0 0 MISSING MEDIUM (global)
semgrep 0 1 0 0 0 0 1 FAILED MEDIUM (global)
syft 0 0 0 0 0 0 0 MISSING MEDIUM (global)

Top 2 Hotspots

Files with the highest number of security findings:

Finding Count File Location
1 .github/workflows/workflow.yml
1 src/stickler/reporting/html/interactive/main.js

Detailed Findings

Show 2 actionable findings

Finding 1: CKV2_GHA_1

  • Severity: HIGH
  • Scanner: checkov
  • Rule ID: CKV2_GHA_1
  • Location: .github/workflows/workflow.yml:11-12

Description:
Ensure top-level permissions are not set to write-all


Finding 2: javascript.browser.security.insecure-document-method.insecure-document-method

  • Severity: HIGH
  • Scanner: semgrep
  • Rule ID: javascript.browser.security.insecure-document-method.insecure-document-method
  • Location: src/stickler/reporting/html/interactive/main.js:407-414

Description:
User controlled data in methods like innerHTML, outerHTML or document.write is an anti-pattern that can lead to XSS vulnerabilities

Code Snippet:

pdfItem.innerHTML = `
                <div class="pdf-container">
                    <canvas id="pdf-canvas-${escapeHtml(docId)}" class="pdf-canvas"></canvas>
                    <div class="pdf-loading" id="pdf-loading-${escpeHtml(docId)}">Loading PDF...</div>
                    <div class="pdf-error" id="pdf-error-${escapeHtml(docId)}" style="display: none;">Error loading PDF</div>
                </div>
                <p><strong>${escapeHtml(docId)}</strong></p>
            `;

Report generated by Automated Security Helper (ASH) at 2025-12-03T05:59:29+00:00

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread pyproject.toml
requires = ["setuptools >= 61.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also seen this done with a pytest.ini file that we may consider using. The pyproject.toml file can become cluttered with configuration over time if every tool uses it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I think that'd be great too but might pollute the root with more files. Would be good to keep within the pyproject.toml for now but definitely valuable to consider a separate pytest.ini as we add more tools and options

@adiadd

adiadd commented Dec 3, 2025

Copy link
Copy Markdown
Contributor Author

Running ruff to lint and format, I'm finding a lot of changes and recommendations - going to open a separate PR afterwards off of dev to handle those separately

@ayushi1208 ayushi1208 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

score = self.comparator.compare("Hello", "hello")
if score < 1.0:
self.assertLess(score, 1.0) # Should be case sensitive
assert score < 1.0 # Should be case sensitive

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on this comment

@ayushi1208
ayushi1208 self-requested a review December 3, 2025 21:52
@adiadd
adiadd merged commit 704699a into dev Dec 3, 2025
@adiadd
adiadd deleted the fix/standardize-tests branch December 3, 2025 21:54
hayleypark pushed a commit that referenced this pull request Jan 28, 2026
* test: standardize tests from unittest to pytest

---------

Co-authored-by: Aditya Addepalli <adiadd@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants