PyJest — a simple implementation of Jest via python

Eyal Segal
1 min readNov 28, 2020

--

When I started working at Aidoc as SRE a year ago, I moved from writing code in JavaScript to Python. The move was easy. Very easy. It felt like writing the pseudo-code I learned in the Introduction to Algorithms course: few words and straightforward syntax.

However, let’s face it: Testing in JavaScript using jest is so much fun. It’s readable. It’s simple. Writing TDD code in JS and jest is easy and enjoyable.

Look at this python — unittest code:

And at this js-Jest code:

Why can’t it be as simple as that in python too?
I’ve written a simplified implementation of jest in python to explore this direction in testing in python.

Well, it can look like that:

To do so, we should start with the “expect” function, that returns an object, with the “jest” methods:

The tester class we want to implement is the “pyJest” this is the lean version of it:

The tester logger is an abstract class with the following abstract methods: write, _success_writer, and _failure_writer, and one router method —conditional_logger_writer :

In other words, the “expect” method returns an object with the expression as an attribute and a collection of methods that can access the expression.

In conclusion, writing tests in python can be as enjoyable as in JS, with a simple one-time effort of implementing the jest package in python. I hope this simple implementation example will inspire some of you to try this yourself,
Have Fun!

--

--