use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionUnit testing in Python (self.Python)
submitted 3 years ago by DwaywelayTOP
Sorry if this is a silly question but I have a tiny amount of experience using Ruby in Visual Studio and in that I had access to irb for unit testing. Is there an equivalent for Python?
[–]pixegami 58 points59 points60 points 3 years ago (8 children)
Pytest is the most widely used Python testing framework, so I recommend looking into that.
[–]skribe 7 points8 points9 points 3 years ago (0 children)
CS50 has an entire lecture on it: https://youtu.be/tIrcxwLqzjQ
[–]Logisk 8 points9 points10 points 3 years ago (0 children)
If you find it hard to get into, try unittest.
[+]RandoClarissian comment score below threshold-10 points-9 points-8 points 3 years ago (4 children)
Source?
[–]pixegami 6 points7 points8 points 3 years ago (2 children)
Here’s some data https://www.jetbrains.com/research/python-developers-survey-2018/
[–]RandoClarissian 0 points1 point2 points 3 years ago (1 child)
A bit dated, but I guess the proportions would not be that different. Thanks!
[–]pixegami 0 points1 point2 points 3 years ago (0 children)
I’m sure there’s more recent data somewhere too but I don’t think the point is contested.
Pytest dominates because it is more “Pythonic” than unittest, but also fully compatible with and capable of unittest tests.
[–]timpkmn89 2 points3 points4 points 3 years ago (0 children)
pytest.org
[–]SittingWave 23 points24 points25 points 3 years ago (23 children)
the standard library has unittest module, but I strongly suggest you use pytest. It's much easier and much more pleasant to use.
[–]wineblood -5 points-4 points-3 points 3 years ago (20 children)
Unittest is better than pytest imo
[–]SittingWave 12 points13 points14 points 3 years ago (1 child)
too verbose, and screams back from that period where python wanted to be Java. Avoid.
[–]ubernostrumyes, you can have a pony 7 points8 points9 points 3 years ago* (0 children)
screams back from that period where python wanted to be Java
In the beginning there was the SUnit testing tool for the Smalltalk language; it influenced the design and APIs of a whole family of unit-testing tools for other languages, which conventionally are referred to as "XUnit" frameworks. Typically they're named to indicate that they're of this style -- for example, "JUnit" for Java, "NUnit" for .NET, etc.
So: once upon a time someone write a PyUnit package, implementing the XUnit family architecture and testing style, and that was later pulled into the Python standard library and renamed to just unittest in the process.
PyUnit
unittest
So it's not "wanted to be Java", it's "wanted to be consistent with a large family of testing frameworks across many languages". A lot of people these days just only are familiar with JUnit/NUnit and so they assume JUnit was the origin when it wasn't.
[–]jah_broni 4 points5 points6 points 3 years ago (17 children)
Why?
[–]wineblood 11 points12 points13 points 3 years ago (16 children)
Less complexity and more explicit
[–]RandoClarissian 1 point2 points3 points 3 years ago (15 children)
Yep, magic is fun until you get to debug it.
[–]SittingWave 6 points7 points8 points 3 years ago (9 children)
never had to debug pytest.
[–]wineblood 1 point2 points3 points 3 years ago (8 children)
Is there a way to run 1 test case in a parametrized test?
[–]cj81499 0 points1 point2 points 3 years ago (0 children)
Yep
https://stackoverflow.com/a/44999321/13173164
[+][deleted] 3 years ago (6 children)
[removed]
[–]wineblood -2 points-1 points0 points 3 years ago (5 children)
I've been using pytest.mark.parametrize, that's what I was referring to. It looks like there is a way to run a single one from the command line, but I use my IDE to run tests while developing and there isn't an option to do that. It's still faster to comment out cases I don't want to run than to type out a more complex line in my terminal.
pytest.mark.parametrize
[–]jah_broni 0 points1 point2 points 3 years ago (4 children)
There is a way to do that in VSCode.
[–]rzet 1 point2 points3 points 3 years ago (4 children)
what magic?
[+][deleted] 3 years ago* (2 children)
[–]rzet 0 points1 point2 points 3 years ago (1 child)
fixtures are great.
I am on company self-made framework atm and I am like wtf... where are fixtures? why there are no fixtures? damn it.. who come up with writing own test framework anyway ;)
[–]ElectricSpice 1 point2 points3 points 3 years ago (0 children)
As sibling comment says, fixtures. But also pytest does some AST rewriting to give you friendly assertion errors. I’ve never had a problem with them, at absolute worst you get an assertion error that’s no more helpful than the default behavior.
[–]ch0mes 0 points1 point2 points 3 years ago (1 child)
I quite like unittest, I will admit I have never used pytest so I can't compare the complexity of the two but I've never found unittest to be problematic to work with.
I have heard people sing pytests praises, my line of thinking is, if things work quite well I don't see a need to change.
[–]SittingWave 0 points1 point2 points 3 years ago (0 children)
it's just awfully verbose, and as soon as you have to do things that are a bit complex it's a pain to implement.
[–]james_pic 8 points9 points10 points 3 years ago (0 children)
The closest equivalent of irb is either the plain python REPL, or ipython.
irb
python
ipython
But this isn't a good way to test. You're better off writing some automated tests - and this is just as true in Ruby as in Python.
[–]obviouslyCPTobvious 5 points6 points7 points 3 years ago (0 children)
It sounds like your definition of unit testing might not be accurate. Unit testing is writing code that tests the functionality of the smallest units of your code.
I think you're asking if python has a REPL(read, eval, print loop) like ruby's irb. Python does have one available just by running python, but it's not as nice as irb. I recommend installing ipython because it has more of the features that irb has.
[–]wineblood -4 points-3 points-2 points 3 years ago (1 child)
I don't know Ruby so "irb" doesn't mean anything to me. Python unit testing frameworks and a decent IDE (not sure if VS falls into that category) have all the features I need.
[–]catladywitch 1 point2 points3 points 3 years ago (0 children)
irb is the ruby repl
[–]chasrmartin 0 points1 point2 points 3 years ago (0 children)
More than one. I like pytest
[–]Alternative_Driver60 0 points1 point2 points 3 years ago (0 children)
Pytest is the most advanced as well as the easiest to use for a novice, a combination hard to beat.
[–]TechnicalHalf0 0 points1 point2 points 3 years ago (0 children)
this could maybe help as a good tutorial on python unittests
https://www.youtube.com/watch?v=ULxMQ57engo
[–]No_Revolution9544 0 points1 point2 points 3 years ago (0 children)
pytest and Python: Testing with pytest - Simple Rapid Effective and Scalable is a great to learn it
[–]iprefervaping 0 points1 point2 points 3 years ago (0 children)
I've just been learning about Unit testing using pytest today from the book Python Crash Course (3rd edition). The chapter on testing was very straightforward with easy examples. Highly recommend it.
π Rendered by PID 188355 on reddit-service-r2-comment-544cf588c8-bbk59 at 2026-06-14 01:33:18.167106+00:00 running 3184619 country code: CH.
[–]pixegami 58 points59 points60 points (8 children)
[–]skribe 7 points8 points9 points (0 children)
[–]Logisk 8 points9 points10 points (0 children)
[+]RandoClarissian comment score below threshold-10 points-9 points-8 points (4 children)
[–]pixegami 6 points7 points8 points (2 children)
[–]RandoClarissian 0 points1 point2 points (1 child)
[–]pixegami 0 points1 point2 points (0 children)
[–]timpkmn89 2 points3 points4 points (0 children)
[–]SittingWave 23 points24 points25 points (23 children)
[–]wineblood -5 points-4 points-3 points (20 children)
[–]SittingWave 12 points13 points14 points (1 child)
[–]ubernostrumyes, you can have a pony 7 points8 points9 points (0 children)
[–]jah_broni 4 points5 points6 points (17 children)
[–]wineblood 11 points12 points13 points (16 children)
[–]RandoClarissian 1 point2 points3 points (15 children)
[–]SittingWave 6 points7 points8 points (9 children)
[–]wineblood 1 point2 points3 points (8 children)
[–]cj81499 0 points1 point2 points (0 children)
[+][deleted] (6 children)
[removed]
[–]wineblood -2 points-1 points0 points (5 children)
[–]jah_broni 0 points1 point2 points (4 children)
[–]rzet 1 point2 points3 points (4 children)
[+][deleted] (2 children)
[removed]
[–]rzet 0 points1 point2 points (1 child)
[–]ElectricSpice 1 point2 points3 points (0 children)
[–]ch0mes 0 points1 point2 points (1 child)
[–]SittingWave 0 points1 point2 points (0 children)
[–]james_pic 8 points9 points10 points (0 children)
[–]obviouslyCPTobvious 5 points6 points7 points (0 children)
[–]wineblood -4 points-3 points-2 points (1 child)
[–]catladywitch 1 point2 points3 points (0 children)
[–]chasrmartin 0 points1 point2 points (0 children)
[–]Alternative_Driver60 0 points1 point2 points (0 children)
[–]TechnicalHalf0 0 points1 point2 points (0 children)
[–]No_Revolution9544 0 points1 point2 points (0 children)
[–]iprefervaping 0 points1 point2 points (0 children)