0

So here is my code:

import unittest

class Primes:
    @staticmethod
    def first(n):
    <python code here>

class Test(unittest.TestCase):
    def __init__(self):
        pass
    def assert_equals(self, l, m):
        self.assertEqual(l, m)

Test = Test()
Test.assert_equals(Primes.first(1), [2])

Whenever I run my code, I get the following error:

Traceback (most recent call last):
  File "firstNPrimes.py", line 37, in <module>
    Test.assert_equals(Primes.first(1), [2])
  File "firstNPrimes.py", line 34, in assert_equals
    self.assertEqual(l, m)
  File "/usr/lib/python3.4/unittest/case.py", line 796, in assertEqual
    assertion_func = self._getAssertEqualityFunc(first, second)
  File "/usr/lib/python3.4/unittest/case.py", line 777, in _getAssertEqualityFunc
    asserter = self._type_equality_funcs.get(type(first))
AttributeError: 'Test' object has no attribute '_type_equality_funcs'

I don't understand what the problem is here.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
codehitman
  • 1,148
  • 11
  • 33
  • What made you think that was the correct way to actually execute the tests? You're bypassing all of the machinery that makes it work, you can't just call the test method directly. Read [the docs](https://docs.python.org/3/library/unittest.html#basic-example). – jonrsharpe Aug 14 '16 at 17:37
  • New to unit-testing in Python. Looked up a few examples to get the ground running. I'm trying to solve a question on CodeWars, so just replicated their tests to try solutions on my computer before I post it online. – codehitman Aug 14 '16 at 17:39
  • Your test should look like `self.assertEqual(Primes.first(1), [2])`, and you should run it with `unittest.main` rather than instantiating the `TestCase` and calling its methods. Then the test setup and discovery is done for you. Otherwise you might as well just write `assert Primes.first(1) == [2]`. – jonrsharpe Aug 14 '16 at 17:41
  • But If I have more than one test would I include all of them under the same method or different methods, e.g., `test_p1`, `test_p2`, etc? – codehitman Aug 14 '16 at 17:45
  • Yes, you'd have a method per test. I have posted an example below. – jonrsharpe Aug 14 '16 at 17:47
  • 2
    If you want to setup your environment like CodeWars, this is the link to the CodeWars Test module: https://github.com/ChristianECooper/CodeWars-Python-TestFramework. Hope it helps! – mpampana Aug 14 '16 at 17:51

2 Answers2

2

You get the error because you're using unittest incorrectly. Per the example in the docs, your tests should look like:

import unittest

class TestPrimes(unittest.TestCase):

    def test_singlePrime_returnsListContainingTwo(self):
        self.assertEqual(Primes.first(1), [2])

    def test_whateverCase_expectedOutcome(self):
        self.assertEqual(Primes.first(...), ...)

if __name__ == '__main__':  # optional, but makes import and reuse easier
    unittest.main()

You can't just instantiate the test case class yourself and call the methods, that skips all of the test discovery and setup.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

Your methods look fine to me, and mimic what I see in the example code provided through the help function.

import unittest help(unittest)

I have the same problem using a debs build in a virtual box working in a python 2.7 environment. For some reason only the assertEqual method is problematic. assertAlmostEqual (scientific assert equal), assertSequenceEqual, assertItemEqual, etc. have no problems.

Since you are subclassing unittest.TestCase the class you define is inheriting all the methods of unittest.TestCase class, including the method assertEqual.

for your code I can run (from the command line in python 2.7):

import testing_code as t

test_object = t.Test()

t.assertSequenceEqual([4,5,6] , [4,5,6])

t.assertNotEqual(4,7)

and no problems... I get your same errors trying a simple assertEqual method. I don't think this is a code structure/misuse of unittest problem, and am pretty sure this is an environment/build problem. I decided i needed an assert equal as a method on my class so i just made a simple one:

 def AssertEqual(self, a, b):
    if a!=b:
        msg= 'inputs unequal: a, b:', a, b
        raise ValueError, msg
Dylan
  • 49
  • 1
  • 3