11.. title: How is PyPy Tested?
22.. slug: how-is-pypy-tested
3- .. date: 2022-03-02 12:00:00 UTC
3+ .. date: 2022-03-09 12:00:00 UTC
44.. tags:
55.. category:
66.. link:
@@ -34,7 +34,8 @@ called "CPython", because it is an interpreter implemented in C code.
3434
3535Now we can make the statement "PyPy is Python in Python" more precise: PyPy is
3636an interpreter for Python 3.9, implemented in RPython. RPython ("Restricted
37- Python") is a subset of Python 2, which is statically typed and can be compiled
37+ Python") is a subset of Python 2, which is statically typed (using type
38+ inference, not type annotations) and can be compiled
3839to C code. That means we can take our Python 3.9 interpreter, and compile it
3940into a C binary that can run Python 3.9 code. The final binary behaves pretty
4041similarly to CPython.
@@ -109,12 +110,15 @@ the same class, also with the value 42.
109110
110111These tests can be run on top of any Python 2 implementation, either CPython or
111112PyPy. We can then test and debug the internals of the PyPy interpreter using
112- familiar tools like indeed pytest and the Python debuggers.
113+ familiar tools like indeed pytest and the Python debuggers. They can be run,
114+ because all the involved code like the tests and the class ``W_IntObject `` are
115+ just completely regular Python 2 classes that behave in the regular way when
116+ run on top of a Python interpreter.
113117
114118In CPython, these tests don't really have an equivalent. They would correspond
115119to tests that are written in C and that can access test the logic of all the C
116120functions of CPython that execute certain functionality, accessing the internals
117- of C structs in the process.
121+ of C structs in the process. ` ¹ `_
118122
119123
120124Application-Level Tests
@@ -125,7 +129,7 @@ don't run on the level of the implementation. Instead, they are executed *by*
125129the PyPy Python interpreter, thus running on the level of the applications run
126130by PyPy. Since the interpreter is running Python 3, the tests are also written
127131in Python 3. They are stored in files with the pattern ``apptest_*.py `` and
128- look like "regular" Python 3 tests. `¹ `_
132+ look like "regular" Python 3 tests. `² `_
129133
130134Here's an example of how you could write a test equivalent to the one above:
131135
@@ -154,7 +158,7 @@ fast, given that they run on a stack of two different interpreters.
154158
155159Application-level tests correspond quite closely to CPython's tests suite (which
156160is using the unittest framework). Of course in CPython it is not possible to run
157- the test suite without building the CPython binary using a C compiler. `² `_
161+ the test suite without building the CPython binary using a C compiler. `³ `_
158162
159163So when do we write application-level tests, and when interpreter-level tests?
160164Interpreter-level tests are necessary to test internal data structures that
@@ -286,7 +290,15 @@ Footnotes
286290
287291.. _`¹` :
288292
289- ¹ There is also a deprecated different way to write these tests, by putting
293+ CPython has the `_testcapimodule.c ` and related modules, that are used to
294+ unit-test the C-API. However, these are still driven from Python tests using
295+ the ``unittest `` framework and wouldn't run without the Python interpreter
296+ already working.
297+
298+
299+ .. _`²` :
300+
301+ ² There is also a deprecated different way to write these tests, by putting
290302them in the ``test_*.py `` files that interpreter level tests are using and
291303then having a test class with the pattern ``class AppTest* ``. We haven't
292304converted all of them to the new style yet, even though the old style is
@@ -295,9 +307,9 @@ Python 2, the tests methods in ``AppTest*`` classes need to be written in the
295307subset of Python 3 that is also valid Python 2 syntax, leading to a lot of
296308confusion.
297309
298- .. _`² ` :
310+ .. _`³ ` :
299311
300- ² Nit-picky side-note: `C interpreters `_ `are a thing `_! But not that
312+ ³ Nit-picky side-note: `C interpreters `_ `are a thing `_! But not that
301313widely used in practice, or only in very specific situations.
302314
303315.. _`C interpreters` : https://root.cern.ch/root/html534/guides/users-guide/CINT.html
0 commit comments