Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unit testing - AssertRaises
#1
Hi, I got this converter:

def nmea_to_dd(coordinate, direction):
    if direction not in ['N', 'S', 'W', 'E']:
        raise TypeError('only N, S, E or W are valid directions')

    if coordinate.find('.') is 5:
        """ longitude in the DDDMM.MMMMM format """
        dd = int(float(coordinate[:3].strip('0')))
        ss = float(coordinate) - dd * 100
        if direction == 'E':
            return round(dd + (ss / 60), 6)
        elif direction == 'W':
            return round(dd + (ss / 60), 6) * -1
        else:
            return 0.0
    if coordinate.find('.') is 4:
        """  latitude in the DDMM.MMMMM format """
        dd = int(float(coordinate) / 100)
        ss = float(coordinate) - dd * 100
        if direction == 'N':
            return round(dd + (ss / 60), 6)
        elif direction == 'S':
            return round(dd + (ss / 60), 6) * -1
        else:
            return 0.0
Here is the unittest file:
import unittest
from converter import *


class TestNmeaConverter(unittest.TestCase):
    def test_valid(self):
        self.assertAlmostEqual(nmea_to_dd('5132.0000', 'N'), 51.533333)
        self.assertAlmostEqual(nmea_to_dd('5132.0000', 'S'), -51.533333)
        self.assertAlmostEqual(nmea_to_dd('01323.629', 'E'), 13.393817)
        self.assertAlmostEqual(nmea_to_dd('01323.629', 'W'), -13.393817)

    def test_directions(self):
        self.assertRaises(TypeError, nmea_to_dd('5132.0000', 'A'))
I dont know how to use assertRaises with 2 parameters in the 13th line. I would like to make sure that the function raises TypeError if it's given another letter as the second parameter.
I will also parse the first parameter's correct form later.

Thanks,
Reply
#2
For assert raises you want to pass the function object, not a call to the function object. That makes it possible for unittest to run the function in an environment where any exceptions can be caught and tested. If you want to set parameters for the call, you pass those parameters to assertRaises as a *args tuple (and/or a **kwargs dictionary).

Try:

    def test_directions(self):
        self.assertRaises(TypeError, nmea_to_dd, ('5132.0000', 'A'))
Note the extra comma after nmea_to_dd.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks, in the meantime I got it working as well:

    def test_input(self):
        with self.assertRaises(TypeError):
            qtime_to_osmand_timestamp(True, False)
Reply
#4
By the way I think appropriate exception is ValueError, not TypeError

Quote:exception ValueError - Raised when an operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.

Quote:exception TypeError

Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.

This exception may be raised by user code to indicate that an attempted operation on an object is not supported, and is not meant to be. If an object is meant to support a given operation but has not yet provided an implementation, NotImplementedError is the proper exception to raise.

Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 425 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  Remove function and unit test ftg 5 3,533 Jan-07-2020, 03:10 PM
Last Post: ndc85430
  Odd Unit Test Behavior ichabod801 3 2,575 Jan-02-2020, 03:34 PM
Last Post: ichabod801
  Define unit of measure of a number doug2019 3 2,377 Oct-15-2019, 03:43 PM
Last Post: jefsummers
  unit testing a method that asks two user inputs() in console gebel 0 2,140 Apr-03-2019, 07:59 PM
Last Post: gebel
  unit test roll die saladgg 5 4,163 Nov-06-2018, 11:39 PM
Last Post: stullis
  Would you unit test __init__ method? kilthar 1 30,998 Oct-18-2017, 05:31 PM
Last Post: snippsat
  Unit testing mp3909 1 2,637 Oct-15-2017, 03:48 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020