pytest-dev/pytest · error · ValueError

pytester.makefile expects a file extension, try .

Error message

pytester.makefile expects a file extension, try .{ext} instead of {ext}

What it means

Thrown by Pytester._makefile() when ext is a non-empty string that does not start with '.'. pytester.makefile() requires the leading dot to avoid silently creating files like 'testpy' instead of 'test.py'.

Solutions

  1. Prefix the extension with a dot: pass '.py' not 'py'.
  2. Use makepyfile()/maketxtfile()/makeini() shortcuts which handle the dot for you.
  3. Normalize the extension: ext = '.' + ext.lstrip('.') before calling.

Example fix

// before
pytester.makefile("py", test_foo="assert True")
// after
pytester.makefile(".py", test_foo="assert True")
// or
pytester.makepyfile(test_foo="assert True")
Defensive patterns

Strategy: validation

Validate before calling

if ext and not ext.startswith('.'):
    ext = '.' + ext
pytester.makefile(ext, content)

Prevention

When it happens

Trigger: Calling pytester.makefile('py', 'content') instead of makefile('.py', ...); passing an extension obtained from Path.suffix (which omits the dot for extensionless files) or from a config value that strips the dot.

Common situations: Muscle memory from other APIs that want 'py' not '.py'; reading extensions from config files that store them without the dot; refactoring where makepyfile() was replaced by the generic makefile().

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/d199c507493fb2c0. Report an issue: GitHub.

Appendix: source

Thrown at src/_pytest/pytester.py:772

        This is done automatically upon instantiation.
        """
        self._monkeypatch.chdir(self.path)

    def _makefile(
        self,
        ext: str,
        lines: Sequence[Any | bytes],
        files: Mapping[str, _FileContent],
        encoding: str = "utf-8",
    ) -> Path:
        items = list(files.items())

        if ext is None:
            raise TypeError("ext must not be None")

        if ext and not ext.startswith("."):
            raise ValueError(
                f"pytester.makefile expects a file extension, try .{ext} instead of {ext}"
            )

        def to_text(s: Any | bytes) -> str:
            return s.decode(encoding) if isinstance(s, bytes) else str(s)

        if lines:
            source = "\n".join(to_text(x) for x in lines)
            basename = self._name
            items.insert(0, (basename, source))

        ret = None
        for basename, value in items:
            p = self.path.joinpath(basename).with_suffix(ext)
            p.parent.mkdir(parents=True, exist_ok=True)
            source_ = Source(value)
            source = "\n".join(to_text(line) for line in source_.lines)
            p.write_text(source.strip(), encoding=encoding)

View on GitHub (pinned to 0d6fbdeffa)