From Jest to Pytest: A JavaScript Developer's Journey into Python Testing
As a developer with a JavaScript background, I've spent a fair amount of time writing tests with , I had to play around with some experimental features with that taught me how to run Jest tests when using ES6 syntax. Without the wisdom of these smart individuals, let's just say I would have struggled! However, armed with this knowledge of how to use Jest's experimental features, I was able to:
- write my tests in Jest (despite my weird configurations)
- set up my
CI pipelinevery early on, which is set up to:
lintmy code (i.e., check for any potential lines of code that can be problematic in the future or can cause bugs as they are now)
testmy source code (i.e., runs tests that I have written which explain how my source code should work).
The code for the CI pipeline I described above can be found . Aryan's project is a command-line tool called ) was definitely outside my comfort zone, but I really appreciated that Aryan was willing to show me the ropes (thanks Aryan!).
Testing Python code with pytest
When I started working on tests for Aryan's repository, I was immediately overwhelmed by how different the tests looked. While Jest had become familiar territory for me, Python's pytest felt very foreign. Nevertheless, with Aryan's guidance and some determination, I started to understand its unique features.
Let's break down what I discovered in their test cases:
Parameterized Testing: One of the first things that caught my eye was the @pytest.mark.parametrize decorator. This is similar to Jest's test.each, but with a cleaner syntax:
@pytest.mark.parametrize(
'invalid_url',
[
'https://gitlab.com/username/repository',
'https://github.com/username',
# ... more test cases
],
)
Context Managers: Instead of Jest's expect().toThrow(), Python uses context managers with pytest.raises:
with pytest.raises(typer.BadParameter, match='Invalid GitHub repository URL'):
check_cli_arguments(invalid_url, 'gemini', 0.5, Path('output.md'))
Temporary File System: The tests use pytest's tmp_path fixture for file system operations, which is much cleaner than setting up mock file systems in Jest:
def test_output_file_is_directory(self, tmp_path):
# tmp_path is automatically provided and cleaned up
Final Thoughts
This experience of working with both JavaScript and Python testing frameworks has broadened my perspective on software testing. While Jest felt like home territory, I've come to appreciate pytest's powerful features like parameterized testing and fixtures. Whether you're writing JavaScript or Python tests, the end goal remains the same: delivering reliable, well-tested code to your users.
SOCIAL SHARE CARD GENERATOR