[Tutor] unittest difficulty

Shall, Sydney sydney.shall at kcl.ac.uk
Wed Jul 19 07:35:38 EDT 2017

I am learning to use unittest.

I have written a program that runs as it should.
247 tests give me a satisfactory answer.

I have now added one more test and I get an error which I do not understand.

The two relevant tests are:

  def test_type_capitalsadvanced(self):
         self.assertEqual(type(self.capitalsadvanced), numpy.ndarray)

  def test_zero_in_capitalsadvanced(self):
         self.assertIn(self.capitalsadvanced, 0.0)

The error message is:

Traceback (most recent call last):
line 320, in test_zero_in_capitalsadvanced
     self.assertIn(self.capitalsadvanced, 0.0)
   File "/Users/sydney/anaconda/lib/python3.6/unittest/case.py", line 
1077, in assertIn
     if member not in container:
TypeError: argument of type 'float' is not iterable

Final output from the tests is :

Ran 247 tests in 1.179s

FAILED (failures=9, errors=1)

The failures all arise from a 'nan'.
It is this problem that I am trying to resolve.

My problem is that the first test tells me correctly that the object 
capitalsadvanced is a numpy.ndarray. But the second test error message 
says it is a float.

I should add that the program creates the initial data set by making use 
of the random function which is given a mean to work with. Thus each 
test run will be with different input data. But repeated tests show the 
same errors.

Any guidance will be very welcome.



Professor Sydney Shall
Department of Haematology/Oncology
Phone: +(0)2078489200
E-Mail: sydney.shall
[Correspondents outside the College should add @kcl.ac.uk]

More information about the Tutor mailing list