pytest-dev / pytest-dev/pytest

fixture function `makepyfile` does not encode files properly

Open
#13,331 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

makepyfile is ignoring the encoding parameter, in both pytester and testdir fixtures.

found the issue working on windows with cp1252 encoding and using pytest-doctestplus.

i traced the issue to makepyfile, arguments and kwargs are passed unpacked.

 make_file = testdir.makepyfile(
            f"""
            def f():
                '''
                >>> print('{a}')
                {b}
                '''
                pass
            """.encode(encoding=encoding),
            encoding=encoding, # will be packed into `files` and default utf-8 encoding will be used
        )
$ python --version
Python 3.13.2

$ pip freeze
colorama==0.4.6
iniconfig==2.0.0
packaging==24.2
pluggy==1.5.0
pytest==8.3.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/_pytest/pytester.py at makepyfile, then compare how the pytester and testdir fixtures pass arguments and keyword arguments into it. Check the existing tests around these fixtures and add coverage for a non-UTF-8 encoding such as cp1252. Done means the encoding parameter is honored by both fixtures when creating files.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.