1
1
.. title: How is PyPy Tested?
2
2
.. slug: how-is-pypy-tested
3
- .. date: 2022-03-02 12:00:00 UTC
3
+ .. date: 2022-03-09 12:00:00 UTC
4
4
.. tags:
5
5
.. category:
6
6
.. link:
@@ -34,7 +34,8 @@ called "CPython", because it is an interpreter implemented in C code.
34
34
35
35
Now we can make the statement "PyPy is Python in Python" more precise: PyPy is
36
36
an 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
38
39
to C code. That means we can take our Python 3.9 interpreter, and compile it
39
40
into a C binary that can run Python 3.9 code. The final binary behaves pretty
40
41
similarly to CPython.
@@ -109,12 +110,15 @@ the same class, also with the value 42.
109
110
110
111
These tests can be run on top of any Python 2 implementation, either CPython or
111
112
PyPy. 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.
113
117
114
118
In CPython, these tests don't really have an equivalent. They would correspond
115
119
to tests that are written in C and that can access test the logic of all the C
116
120
functions of CPython that execute certain functionality, accessing the internals
117
- of C structs in the process.
121
+ of C structs in the process. ` ¹ `_
118
122
119
123
120
124
Application-Level Tests
@@ -125,7 +129,7 @@ don't run on the level of the implementation. Instead, they are executed *by*
125
129
the PyPy Python interpreter, thus running on the level of the applications run
126
130
by PyPy. Since the interpreter is running Python 3, the tests are also written
127
131
in 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. `² `_
129
133
130
134
Here's an example of how you could write a test equivalent to the one above:
131
135
@@ -154,7 +158,7 @@ fast, given that they run on a stack of two different interpreters.
154
158
155
159
Application-level tests correspond quite closely to CPython's tests suite (which
156
160
is 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. `³ `_
158
162
159
163
So when do we write application-level tests, and when interpreter-level tests?
160
164
Interpreter-level tests are necessary to test internal data structures that
@@ -286,7 +290,15 @@ Footnotes
286
290
287
291
.. _`¹` :
288
292
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
290
302
them in the ``test_*.py `` files that interpreter level tests are using and
291
303
then having a test class with the pattern ``class AppTest* ``. We haven't
292
304
converted 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
295
307
subset of Python 3 that is also valid Python 2 syntax, leading to a lot of
296
308
confusion.
297
309
298
- .. _`² ` :
310
+ .. _`³ ` :
299
311
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
301
313
widely used in practice, or only in very specific situations.
302
314
303
315
.. _`C interpreters` : https://root.cern.ch/root/html534/guides/users-guide/CINT.html
0 commit comments