Alex Alex - 3 months ago 8
Python Question

Python. Function testing using Nose

I have just begun learning Python and I got stuck (1day experience :)). Couldn't you help me with my homework?

Exercise:
We have module checkers with function is_triangle

The method signature with a documentation string:

def is_triangle(a, b, c):

"""

:param a: length of first side

:param b: length of second side

:param c: length of third side

:return: "True" if possible to create triangle with these sides. Otherwise "False"

"""


You should develop full set of tests that will verify this function.
The solution should use the nosetest library and it should be carried out in one file like:

$ Python% your_file_name% .py

What should I write in this .py file?

Answer

In this .py file you should write

import nose.tools as nt

from checkers import is_triangle


def test_is_triangle():
    # Define test_a, test_b, and test_c here such that they should produce a
    # return value of False.
    nt.assert_false(is_triangle(test_a, test_b, test_c))

    # Redefine test_a, test_b, and test_c here such that they should produce a
    # return value of True.
    nt.assert_true(is_triangle(test_a, test_b, test_c))

You should then run this script in one of two ways:

$ nosetests your_file_name.py

or

$ python your_file_name.py  # as your instructor has requested.

The test should fail until you have a properly written is_triangle function. If you find that your initial tests are inadequate -- such that the tests pass even though is_triangle is incorrect -- add more.

Comments