DEV Community

Peter Wan
Peter Wan

Posted on

# From Jest to Pytest: A JavaScript Developer's Journey into Python Testing

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 Jest. In my project, gimme_readme, I had to play around with some experimental features with Node and Jest because of the 3rd party npm modules I was using. I was able to find great Stack Overflow threads 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:

  1. write my tests in Jest (despite my weird configurations)
  2. set up my CI pipeline very early on, which is set up to:
    1. lint my 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)
    2. test my 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 here, and is set to run whenever a push is made to any branch or when there's a pull request. This way, anyone who's trying to contribute to my repository will know if the code they contributed is "good enough to go" with regards to merging it into my main branch - at least as far as automated tests go.

Anywho, that's enough about the work I've done for my repository way back when.

This week, I decided to take on the challenge of writing some tests for a Python project written by my friend Aryan Khurana. Aryan's project is a command-line tool called github-echo which provides insights on a GitHub repository. Using an unfamiliar language + a testing framework I had never used (PyTest) 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
       ],
   )
Enter fullscreen mode Exit fullscreen mode

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'))
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)