Mike Driscoll Profile picture
Nov 19, 2021 β€’ 13 tweets β€’ 4 min read β€’ Read on X
Today we are going to talk about the basics of unit testing in #Python

πŸ§΅πŸπŸ‘‡
The Python programming language includes built-in packages for testing:

🐍 doctest
🐍 unittest
I already did a thread on Python's `doctest`:

To get started with unit testing in Python, you need some code to unit test

Here's some code you can use that you can save as "mymath.py"
To add a unit test in Python, you normally create a new file with the same name as the file you are testing, but prepended with the word "test" -> "test_mymath.py"

Then you subclass `unittest.TestCase` and create one or more test methods

Here's an example:
To keep things simple, save the test file in the same directory as the file that you are testing.

Then open up a terminal and navigate to the folder that has your code.

Finally, you run the following command:
What does this output mean? You will see dots or periods for each test that passes and "F"s for tests that fail.

In this example, you have 3 tests and 3 periods, so they all passed!
If you'd like more information about what tests are running, you can pass the `-v` argument for verbose mode:
You can get a full listing of the arguments you can pass to your test by using `-h` for help:
You can even specify which tests you want it to run specifically rather than running all of them:
You can do a lot with `unittest`. Check out the full documentation for details:

docs.python.org/3/library/unit…
The examples in this thread come from my unittest tutorial on @mousevspython

My tutorial also covers:

🐍 More complex tests
🐍 Test suites
🐍 Skipping tests
🐍 Integrating with doctest

blog.pythonlibrary.org/2016/07/07/pyt…
I hope you enjoyed learning the basics of unit testing with Python.

Follow me for more great Python content!

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

Feb 1
Did you know that #Python supports SQLite in the standard library? That's right. You don't need to install anything! πŸπŸš€

Let's talk a little about that!

πŸ§΅πŸ‘‡
If you have a pre-existing SQLite database file, you can use #Python's `sqlite3` module to connect to it

Here's how: Image
If you want to create a new SQLite database with #Python, you can use the same syntax that you'd use to connect to a pre-existing one.

Then create a cursor object and create your table, like this: Image
Read 8 tweets
Nov 21, 2023
Today is a great day to talk about #Python lambdas!

Let's learn about them in a thread

πŸπŸ§΅πŸ‘‡
A Python lambda is a one-line anonymous function.

Rule #1 is do NOT assign a lambda to a variable.

When you do that, it's no longer an anonymous function. You should just create a regular function at that point Image
If you want to run a lambda in your REPL, wrap it in parentheses, like this: Image
Read 9 tweets
Mar 23, 2023
Want to create a copy of a #Python list? Use Python's `copy()` method!

Note: Watch out if your list contains lists of dictionaries. In those cases, you might be better off using copy.deepcopy()
But be careful! If your list contains a mutable object, like another list or a dictionary, you may encounter some unexpected behavior.

In the following example, you `copy()` the list. Then you modify the nested dictionary in the copy, but that also changes the original list!
You can fix this behavior by using Python's `copy` module. It provides a deepcopy() function that you can use which will make a deep copy of the ENTIRE list!
Read 4 tweets
Mar 22, 2023
#Python generators aren't talked about enough.

So let's talk about them today!

πŸ§΅πŸπŸ‘‡
According to the #Python wiki, "Generator functions allow you to declare a function that behaves like an iterator"

One of their best use cases is to chunk through large data sets a piece at a time, which prevents you from running out of memory.

wiki.python.org/moin/Generators
To create a generator in #Python, you need to use the `yield` keyword instead of `return` inside a function.

Generators will continue to yield values until the generator is exhausted.

The following example will continue to yield numbers for as long as you call it: Image
Read 10 tweets
Mar 21, 2023
My 11th #Python book is now available! 🐍🀯

Test your Python skills and knowledge with over 100 PYthon quizzes. Answers are included if you get stuck!

Available on Amazon, Leanpub, and Gumroad (see following tweets)
Get my 11th #Python book, The Python Quiz book on Amazon in the following formats:

🐍 Kindle
🐍 Paperback

amazon.com/dp/B0BVT8DZV1
You can find The Python Quiz Book on @gumroad too in the following formats:

🐍 epub
🐍 PDF

driscollis.gumroad.com/l/pyquiz
Read 4 tweets
Mar 17, 2023
Starting in Python 3.7, the `breakpoint()` built-in function was added

It is defined in PEP 553 and simplifies adding a breakpoint to your code

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
You can read about the `breakpoint()` function for Python here:

python.org/dev/peps/pep-0…
Here is some sample code showing how to use the `breakpoint()` function in your Python code
Read 6 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(