


# From Jest to Pytest: A JavaScript Developers 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:
- write my tests in Jest (despite my weird configurations)
- set up my CI pipeline very early on, which is set up to:
- 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)
- 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 ], )
Context Managers: Instead of Jest's expect().toThrow(), Python uses context managers with pytest.raises:
@pytest.mark.parametrize( 'invalid_url', [ 'https://gitlab.com/username/repository', 'https://github.com/username', # ... more test cases ], )
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:
with pytest.raises(typer.BadParameter, match='Invalid GitHub repository URL'): check_cli_arguments(invalid_url, 'gemini', 0.5, Path('output.md'))
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.
The above is the detailed content of # From Jest to Pytest: A JavaScript Developers Journey into Python Testing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

Fastapi ...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...
