Categories
Python Answers

How to generate dynamic (parameterized) unit tests in Python?

Sometimes, we want to generate dynamic (parameterized) unit tests in Python.

In this article, we’ll look at how to generate dynamic (parameterized) unit tests in Python.

How to generate dynamic (parameterized) unit tests in Python?

To generate dynamic (parameterized) unit tests in Python, we can use the parameterized module.

To install it, we run

pip install parameterized

Then we can use it by writing

from parameterized import parameterized

class TestSequence(unittest.TestCase):
    @parameterized.expand([
        ["foo", "a", "a",],
        ["bar", "a", "b"],
        ["lee", "b", "b"],
    ])
    def test_sequence(self, name, a, b):
        self.assertEqual(a, b)

to call parameterized.exapnd decorator method with a list of lists of values to create our test with.

We use it to modify the test_sequence to create the parameterized tests.

The first value in each list is appended to the test name.

The rest of the arguments are used in our tests.

Conclusion

To generate dynamic (parameterized) unit tests in Python, we can use the parameterized module.

Categories
Python Answers

How to access dict keys like an attribute with Python?

Sometimes, we want to access dict keys like an attribute with Python.

In this article, we’ll look at how to access dict keys like an attribute with Python.

How to access dict keys like an attribute with Python?

To access dict keys like an attribute with Python, we can create a subclass of the dict class.

For instance, we write

class AttributeDict(dict):
    __slots__ = ()
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__

to create the AttributeDict subclass of dict that sets the __getattr__ and __setattr__ methods to the dict‘s __getitem__ and __setitem__ methods respectively.

Now we can manipulate dict entries like objects by creating AttributeDict instances.

Conclusion

To access dict keys like an attribute with Python, we can create a subclass of the dict class.

Categories
Python Answers

How to write a try-except block that catches all exceptions with Python?

Sometimes, we want to write a try-except block that catches all exceptions with Python.

In this article, we’ll look at how to write a try-except block that catches all exceptions with Python.

How to write a try-except block that catches all exceptions with Python?

To write a try-except block that catches all exceptions with Python, we can catch the Exception exception with the except block.

For instance, we write

import traceback
import logging

try:
    whatever()
except Exception as e:
    logging.error(traceback.format_exc())

to catch any exceptions raised by the whatever function when it’s called.

Then we call logging.error to log the traceback of the exception.

Conclusion

To write a try-except block that catches all exceptions with Python, we can catch the Exception exception with the except block.

Categories
Python Answers

How to get a list of locally installed Python modules?

Sometimes, we want to get a list of locally installed Python modules.

In this article, we’ll look at how to get a list of locally installed Python modules.

How to get a list of locally installed Python modules?

To get a list of locally installed Python modules, we can use the help function.

For instance, we write

help('modules')

to call help with 'modules' to get a list of locally installed Python modules.

Conclusion

To get a list of locally installed Python modules, we can use the help function.

Categories
Python Answers

How to fix iterating on a file doesn’t work the second time with Python?

Sometimes, we want to fix iterating on a file doesn’t work the second time with Python.

In this article, we’ll look at how to fix iterating on a file doesn’t work the second time with Python.

How to fix iterating on a file doesn’t work the second time with Python?

To fix iterating on a file doesn’t work the second time with Python, we can use the with keyword.

For instance, we write

with open('foo.html', 'rU') as f:
  for line in f:
     print(line)

to open foo.html.

Then we loop through the lines in the file in the for loop.

Once the loop is done, the file is closed so that we can open it again and run the loop on it again.

Conclusion

To fix iterating on a file doesn’t work the second time with Python, we can use the with keyword.