Python asserts

All

Intro

I was doing some python tests recently I came to use python asserts again, np_asserts, python asserts and unittest framework. All very very useful, but I think the original assert is so clever and easy to use, we should apply them in everyday life.

asserts

  I was comparing the name of a bank (string) with the calculated value of a function that returns it.
   assert get_from_id_name_bank(‘004’) == ‘TD’
  Similar to the example in [1]

np library

Np is also so straightforward, just need to do import numpy as np and it’s done! Put the comparison, and done!

Comparing arrays for example:

      np.testing.assert_array_equal(expected,banks)
    All the supported functions are here [2].

unittest

That’s a complete framework for testing and it’s very simple and has set_up, and tear_down. All in it.

    python -m unittest -v test_module

You can add some verbosity via v while doing the execution

Source

https://github.com/FranciscoMeloJr/Python-Tests/tree/master/

REFs

[1]https://code-maven.com/slides/python-programming/pytest-compare-strings

[2]https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.testing.html

[3] https://docs.python.org/3/library/unittest.html (for python 3)

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s