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

18 Nov
Python comes with its own built-in debugger called `pdb`.

Let's talk about that in a mini-thread!

πŸ§΅πŸπŸ‘‡
`pdb` stands for Python debugger. It is a built-in part of Python's standard library

Here is a link to the documentation:

docs.python.org/3/library/pdb.…
To start debugging code with `pdb`, you need to write some code.

Here's the code you'll be using:
Read 17 tweets
17 Nov
One big gotcha when it comes to #Python decorators is that the decorator will covertly replace the function's name and docstring with its own (πŸ§΅πŸπŸ‘‡)

Check out the following example Image
When you run the decorated code in the previous tweet, you will see that the decorated function's name and docstring have disappeared! Image
You may be wondering, why do I care if a function's name and docstring are replaced?

The reason this is important is that your introspection won't work correctly anymore. If you run Python's `help()` built-in against your decorated function, it will give you the wrong info
Read 7 tweets
17 Nov
#Python has had the concept of context managers for a loooong time!

Let's talk about context managers again!

πŸ§΅πŸπŸ‘‡
The `with` statement, which is the normal way for working with context managers, was added back in Python 2.5!

Here is a pretty common example of using a context manager: Image
The beauty of a context manager is that they allow you to do some setup and teardown automatically.

The downside is that is abstracted away and can sometimes make the code less obvious when debugging
Read 8 tweets
15 Nov
Logging in #Python is a popular topic and it's a really useful one to understand.

Today we are going to learn more about that!

πŸ§΅πŸπŸ‘‡
Python includes the `logging` module as a part of its standard library.

You can get started using it quite easily:
Python's `logging` module supports multiple logging levels:

🐍 notset
🐍 debug
🐍 info
🐍 warning
🐍 error
🐍 critical
Read 17 tweets
12 Nov
One of the harder concepts to learn in #Python are decorators.

So let's take a few moments and learn about them!

πŸ§΅πŸπŸ‘‡
It's always good to start with a regular function. Why?

Because you create a decorator using functions!

This function, `doubler()` takes in a number and doubles it. That's it! Image
But wait! #Python functions are also objects. You can get their `__name__` and their docstring (`__doc__`), if they have one.

You can also get a listing of their other methods with `dir()`

What all this means is that you can pass a function to another function! Image
Read 11 tweets
8 Nov
Did you know you can watermark images with #Python?

Find out how using the #Pillow package in this mini-thread!

πŸ§΅πŸπŸ‘‡
If you want to add text, such as a URL, to an image, you can do that pretty easily with #Python and #pillow

The following code snippet is only 21 lines!
Here are the before and after photos of the Yequina lighthouse showing how to add a watermark with #Python
Read 5 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

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

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(