Testing Files Generator
English

Test frameworks

Two packages bring the files into your tests: testing-files-generator on PyPI for pytest and unittest, and the package of the same name on npm for Playwright, Vitest and node:test. Each gives you one test for each file of a set, named by what the file should meet, and fails a test with a sentence that says what went wrong.

How it works

You choose a set, usually a preset. The package plans it and gives you its files. Each test hands its file to your system and ends with one line saying what your system did. A file with no right answer is skipped, never passed. tfg comes inside both packages, works offline and never connects to the network.

pytest

pip install testing-files-generator

pytest finds the plugin on its own. Python 3.10 or later. This test sends every file of the upload-validation preset to an upload endpoint with requests, around a limit of 2 MB:

# tests/test_upload.py
import pytest
import requests
from testing_files_generator import file_set

uploads = file_set(preset="upload-validation", options={"limit": "2mb", "bulk": 0})


@pytest.mark.parametrize("file", uploads.params())
def test_upload(file):
    with open(file.path(), "rb") as body:
        response = requests.post("http://localhost:3000/upload", files={"file": (file.send_as, body)}, timeout=30)
    assert response.status_code < 500, "a server error is not a refusal"
    file.check("accept" if response.ok else "reject")

Each test is named by its file and what the file expects. Listing them writes no file:

pytest --collect-only -q
tests/test_upload.py::test_upload[2mb_under_1b.jpg - expected accept]
tests/test_upload.py::test_upload[2mb_at_limit.jpg - expected accept]
tests/test_upload.py::test_upload[2mb_over_1b.jpg - expected reject (size_limit)]
tests/test_upload.py::test_upload[pdf_as_jpg.jpg - expected reject (mime_mismatch)]
...

Every test carries its outcome and its group as markers, so pytest -m reject or pytest -m size-limit runs part of the set. Without pytest, the same set works in unittest or any runner that makes tests in a loop.

Playwright

npm install --save-dev testing-files-generator

This spec uploads every file through the form, the way a person does:

// tests/upload.spec.ts
import { test, expect } from '@playwright/test';
import { fileSet } from 'testing-files-generator';

const uploads = fileSet({ preset: 'upload-validation', options: { limit: '2mb', bulk: 0 } });

for (const file of uploads.files) {
  test(file.title, file.details, async ({ page }) => {
    test.skip(file.unspecified, file.whyUnspecified);
    await page.goto('/upload');
    await page.getByLabel('File').setInputFiles(await file.path());
    await page.getByRole('button', { name: 'Upload' }).click();
    const status = page.getByRole('status');
    await expect(status).toHaveText(/Uploaded|Refused/);
    file.check((await status.textContent())?.includes('Uploaded') ? 'accept' : 'reject');
  });
}

Only the lines that upload and read the answer are about your app. Change them to however your app takes a file. Wait for an answer your app gives on purpose, as Uploaded or Refused here, so a page that shows an error fails the test rather than passing as a refusal. npx playwright test --grep @reject runs part of the set.

Vitest and node:test

The package imports nothing from Playwright, so any runner that declares tests in a loop works. With Vitest:

import { test, expect } from 'vitest';
import { openAsBlob } from 'node:fs';
import { fileSet } from 'testing-files-generator';

const uploads = fileSet({ preset: 'upload-validation', options: { limit: '2mb', bulk: 0 } });

for (const file of uploads.files) {
  test.skipIf(file.unspecified)(file.title, async () => {
    const body = new FormData();
    body.append('file', await openAsBlob(await file.path()), file.sendAs);
    const response = await fetch('http://localhost:3000/upload', { method: 'POST', body });
    expect(response.status, 'a server error is not a refusal').toBeLessThan(500);
    file.check(response.ok ? 'accept' : 'reject');
  });
}

With the runner built into Node, node --test, the loop is the same, with test(file.title, { skip: file.unspecified && file.whyUnspecified }, ...).

Telling the set what your system did

check takes accept, reject or sanitize and fails the test when the file expected something else. When your system says why it turned a file away, pass the reason as well, and a file turned away for the wrong rule fails too. In Python:

file.check({"outcome": "reject", "reason": response.json().get("reason")})

A server error is not a refusal, so check the status before check, as the examples do. A file sent under another name, such as ../climbed_out.jpg, gives that name in send_as, or sendAs in Node, and the examples upload it under that name.

Your own set

Any preset works in place of upload-validation, with its settings in options. For a set of your own, write a recipe and name it:

uploads = file_set(recipe="tests/fixtures.yaml")
const uploads = fileSet({ recipe: path.join(__dirname, 'fixtures.yaml') });

Recipes says how to write one, and the recipe gallery has ready ones to start from.

Everything else

Each package has a full guide of its own, with where the files are kept, CI, every option and what to do when something goes wrong: testing-files-generator on PyPI and testing-files-generator on npm.