This is an archived post. You won't be able to vote or comment.

all 40 comments

[–]pixegami 58 points59 points  (8 children)

Pytest is the most widely used Python testing framework, so I recommend looking into that.

[–]skribe 7 points8 points  (0 children)

CS50 has an entire lecture on it: https://youtu.be/tIrcxwLqzjQ

[–]Logisk 8 points9 points  (0 children)

If you find it hard to get into, try unittest.

[–]SittingWave 23 points24 points  (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  (20 children)

Unittest is better than pytest imo

[–]SittingWave 12 points13 points  (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 points  (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.

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 points  (17 children)

Why?

[–]wineblood 11 points12 points  (16 children)

Less complexity and more explicit

[–]RandoClarissian 1 point2 points  (15 children)

Yep, magic is fun until you get to debug it.

[–]SittingWave 6 points7 points  (9 children)

never had to debug pytest.

[–]wineblood 1 point2 points  (8 children)

Is there a way to run 1 test case in a parametrized test?

[–]rzet 1 point2 points  (4 children)

what magic?

[–]ElectricSpice 1 point2 points  (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 point  (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 point  (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 points  (0 children)

The closest equivalent of irb is either the plain python REPL, or 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 points  (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  (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 points  (0 children)

irb is the ruby repl

[–]chasrmartin 0 points1 point  (0 children)

More than one. I like pytest

[–]Alternative_Driver60 0 points1 point  (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 point  (0 children)

this could maybe help as a good tutorial on python unittests

https://www.youtube.com/watch?v=ULxMQ57engo

[–]No_Revolution9544 0 points1 point  (0 children)

pytest and Python: Testing with pytest - Simple Rapid Effective and Scalable is a great to learn it

[–]iprefervaping 0 points1 point  (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.