Skip to content

Conversation

@Sikandar1310291
Copy link

Overview

This PR adds a comprehensive example file demonstrating how to properly use chex.variants with parametrization in pytest. This addresses a common confusion point for users trying to combine chex.variants with test parametrization.

Motivation

Many users encounter issues when attempting to use pytest.mark.parametrize with chex.TestCase because:

  • chex.variants requires test classes to inherit from chex.TestCase (which uses unittest.TestCase)
  • pytest.mark.parametrize does NOT work with unittest.TestCase subclasses
  • There was no clear documentation showing the correct pattern

Related to issue #382

What's Included

The new file chex/_src/variants_pytest_example.py contains four complete test class examples:

1. TestBasicVariants

  • Basic variant usage without parametrization
  • Demonstrates @chex.variants with with_jit and without_jit
  • Shows how to use @self.variant decorator

2. TestCombinedParametrize

  • CRITICAL: Shows that @chex.variants MUST be the OUTSIDE decorator (applied first)
  • Demonstrates using absl.testing.parameterized (which works with unittest)
  • Examples with single and multiple parameters
  • Shows named parameters for better test identification

3. TestMultipleVariants

  • Examples using multiple variant types simultaneously
  • More complex variant combinations

4. Additional examples

  • Advanced patterns and edge cases

Key Takeaways

DO: Use absl.testing.parameterized with chex.variants
DON'T: Use pytest.mark.parametrize with chex.TestCase
⚠️ IMPORTANT: @chex.variants must be the outer decorator

Usage

Run the examples with:

pytest chex/_src/variants_pytest_example.py -v

Testing

All examples have been tested and run successfully with pytest, demonstrating the correct patterns for combining variants with parametrization.

Checklist

  • Added comprehensive example file with 217 lines of code
  • Includes 4 different test class patterns
  • Documented the critical decorator ordering requirement
  • Provided clear usage instructions
  • All examples are functional and tested

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.

2 participants