1---2name: saleor-testing3description: Test Saleor applications — pytest setup, Django test client, GraphQL test patterns, App testing, factory_boy fixtures, and webhook testing. Use when writing tests for Saleor projects.4allowed-tools: Read, Write, Edit, Bash, Grep, Glob, WebSearch, WebFetch5metadata:6 source: https://skillmd.com/skills/orcaqubits/saleor-testing7---89# Saleor Testing1011## Before writing code1213**Fetch live docs**:141. Web-search `site:github.com/saleor/saleor pytest conftest fixtures` for Saleor's test setup and existing fixtures152. Web-search `site:docs.saleor.io app testing webhooks` for App testing patterns and webhook verification163. Web-search `site:docs.saleor.io graphql API testing` for GraphQL query and mutation testing approaches174. Fetch `https://github.com/saleor/saleor/blob/main/conftest.py` and review root-level test configuration185. Web-search `site:docs.pytest.org fixtures factory_boy django` for pytest fixtures and factory_boy integration1920## Test Architecture2122Saleor follows a layered testing approach:2324| Layer | Tool | Purpose |25|-------|------|---------|26| Unit tests | pytest | Test individual functions, utilities, and model methods |27| Integration tests | Django test client | Test GraphQL API endpoints with database |28| App tests | pytest + httpx/requests-mock | Test App webhooks, signature verification |29| E2E tests | pytest + API client | Test full user flows through the API |3031## Pytest Setup3233### Core Configuration3435| File | Purpose |36|------|---------|37| `pytest.ini` / `pyproject.toml` | Pytest settings, markers, default flags |38| `conftest.py` (root) | Shared fixtures, database setup, API clients |39| `conftest.py` (per-app) | App-specific fixtures and helpers |4041### Essential pytest Settings4243| Setting | Value | Purpose |44|---------|-------|---------|45| `DJANGO_SETTINGS_MODULE` | `saleor.tests.settings` | Test-specific Django settings |46| `--reuse-db` | Flag | Reuse test database across runs for speed |47| `--no-migrations` | Flag | Skip migrations; create tables directly |48| `-x` | Flag | Stop on first failure during development |49| `-n auto` | Flag | Parallel execution with pytest-xdist |5051### Key pytest Plugins5253| Plugin | Purpose |54|--------|---------|55| `pytest-django` | Django integration, database access, settings override |56| `pytest-xdist` | Parallel test execution |57| `pytest-mock` | Mock and patch utilities |58| `pytest-asyncio` | Async test support |59| `pytest-vcr` | Record and replay HTTP interactions |60| `pytest-factoryboy` | factory_boy integration with pytest fixtures |6162## Django Test Client for GraphQL6364Saleor's GraphQL API is tested using Django's test client:6566### API Client Pattern6768| Component | Description |69|-----------|-------------|70| Test client | Django `Client` or `RequestFactory` for HTTP requests |71| Endpoint | POST to `/graphql/` with query and variables |72| Authentication | Set `HTTP_AUTHORIZATION` header with JWT or App token |73| Content type | `application/json` for standard queries |7475### Authenticated Request Patterns7677| Actor | Header | Token Source |78|-------|--------|--------------|79| Anonymous | None | No authentication |80| Customer | `Authorization: Bearer <jwt>` | `tokenCreate` or test fixture |81| Staff user | `Authorization: Bearer <jwt>` | Staff user fixture with permissions |82| App | `Authorization: Bearer <app-token>` | App token fixture |8384## GraphQL Test Helper Pattern8586### Response Assertion Patterns8788| Assertion | What to Check |89|-----------|---------------|90| Status code | `assert response.status_code == 200` |91| No errors | `assert "errors" not in content` or `content["data"]["mutation"]["errors"] == []` |92| Data present | `assert content["data"]["query"]["field"] == expected` |93| Permission denied | `assert content["errors"][0]["extensions"]["exception"]["code"] == "PermissionDenied"` |94| Validation error | Check `errors` array in mutation response for field-level errors |9596## factory_boy Factories for Saleor Models9798### Core Model Factories99100| Factory | Model | Key Fields |101|---------|-------|------------|102| `UserFactory` | `User` | email, first_name, last_name, is_staff |103| `ProductTypeFactory` | `ProductType` | name, has_variants, is_shipping_required |104| `ProductFactory` | `Product` | name, product_type, category, slug |105| `ProductVariantFactory` | `ProductVariant` | product, sku, track_inventory |106| `CategoryFactory` | `Category` | name, slug, parent |107| `CollectionFactory` | `Collection` | name, slug |108| `ChannelFactory` | `Channel` | name, slug, currency_code |109| `WarehouseFactory` | `Warehouse` | name, slug, address |110| `OrderFactory` | `Order` | user, channel, billing_address |111| `OrderLineFactory` | `OrderLine` | order, variant, quantity |112| `CheckoutFactory` | `Checkout` | channel, email, shipping_address |113| `VoucherFactory` | `Voucher` | code, type, discount_value |114| `ShippingZoneFactory` | `ShippingZone` | name, countries |115| `ShippingMethodFactory` | `ShippingMethod` | name, type, shipping_zone |116117## Testing Apps118119### Webhook Payload Validation120121| Test Aspect | What to Verify |122|-------------|----------------|123| Payload structure | JSON schema matches expected format |124| Required fields | All mandatory fields are present |125| Data accuracy | Payload values match the triggering event |126| Serialization | Dates, decimals, and enums serialize correctly |127128### Signature Verification Testing129130| Step | Description |131|------|-------------|132| 1. Get key material | For JWS (default): use test JWKS; for legacy HMAC: use App secret key |133| 2. Sign payload | Create valid JWS/HMAC signature for the test payload |134| 3. Set header | Include signature in the `Saleor-Signature` header |135| 4. Verify in App | App verifies signature and processes payload |136| 5. Test mismatch | Verify App rejects requests with invalid signatures |137138## Testing GraphQL Queries and Mutations139140### Query Test Pattern141142| Step | Description |143|------|-------------|144| 1. Create test data | Use factories to set up products, channels, etc. |145| 2. Execute query | Send GraphQL query via test client |146| 3. Assert results | Verify returned data matches created test data |147| 4. Test filtering | Verify filters, search, and pagination work |148| 5. Test permissions | Verify unauthorized users cannot access data |149150### Mutation Test Pattern151152| Step | Description |153|------|-------------|154| 1. Set up prerequisites | Create required related objects |155| 2. Execute mutation | Send mutation with valid input |156| 3. Assert success | Check response for data and no errors |157| 4. Verify database | Query the database to confirm changes persisted |158| 5. Test validation | Send invalid input and verify error messages |159| 6. Test permissions | Verify only authorized users can execute |160161## Fixture Patterns162163### Commonly Needed Test Fixtures164165| Fixture | Provides |166|---------|----------|167| `staff_user` | Authenticated staff user with configurable permissions |168| `customer_user` | Authenticated customer with address |169| `channel_USD` | Default USD channel |170| `product` | Product with type, category, variant, and channel listing |171| `order` | Order with lines, addresses, and payment |172| `checkout` | Checkout with lines and shipping address |173| `warehouse` | Warehouse with stock for test variants |174| `shipping_zone` | Shipping zone with methods and channel listing |175176## CI/CD Pipeline Integration177178| Stage | Tests | Configuration |179|-------|-------|---------------|180| Pre-commit | Linting, type checks | `pre-commit` hooks with `ruff`, `mypy` |181| Unit tests | Fast, isolated tests | `pytest -x --no-migrations -q` |182| Integration tests | API and database tests | `pytest --reuse-db -n auto` |183| Coverage | Code coverage report | `pytest --cov=saleor --cov-report=xml` |184| App tests | Webhook and App tests | `pytest tests/apps/ -v` |185186## Best Practices187188- Use factory_boy factories instead of manual object creation for consistency189- Test both success and error paths for every mutation190- Verify permissions by testing with unauthenticated, customer, and staff users191- Use `@pytest.mark.django_db` on all tests that access the database192- Mock external services (payment gateways, shipping carriers) in unit tests193- Test webhook signature verification (JWS/HMAC) with both valid and invalid signatures194- Keep test fixtures composable and avoid deeply nested dependencies195- Run tests in parallel with `pytest-xdist` for faster CI pipelines196- Use `assertNumQueries` to catch N+1 query problems in resolvers197198Fetch the Saleor testing and pytest documentation for exact fixture patterns, test client setup, and CI configuration before implementing.