Having an issue with Python's unittest
<p>I am learning Python and trying to test a Polynomial class I wrote using unittest. It seems like I am getting different results from directly running a test in Python and running a test using unittest and I don't understand what's going on.</p>

<pre><code>import unittest
from w4_polynomial import Polynomial

class TestPolynomialClass(unittest.TestCase):

    def setUp(self):
        self.A = Polynomial()
        self.A[1] = 1
        self.A[2] = 2
        self.B = Polynomial()
        self.B[1234] = 5678

    def test_assertNotEq(self):
        self.C = Polynomial()
        self.C[1234] = 5678
        self.C[1] = 1
        self.C[2] = 3
        self.assertNotEqual(self.A+self.B, self.C)

if __name__ == '__main__':
    unittest.main()
</code></pre>

<p>Unittest fails... but I don't understand why. The two polynomials aren't the same. Here are the results from the same test done in a python script using print to compare. The polynomial is different, but same results:</p>

<pre><code>A+B =    442x^123 + 12x^6 + 12x^4 + 5x^1 + 0x^0 + -99x^-12 
C =      442x^123 + 12x^6 + 12x^4 + 5x^1 + 0x^0 + 99x^-12 
A+B==C is  False
</code></pre>

<p>Any help explaining what's going on would be greatly appreciated. </p>

<p>Sorry forgot the error from unittest,</p>

<pre><code>    FAIL: test_assertEq (__main__.TestPolynomialClass)
----------------------------------------------------------------------
Traenter code hereceback (most recent call last):
  File "add.py", line 18, in test_assertEq
self.assertNotEqual(self.A+self.B, self.C)
AssertionError: &lt;w4_polynomial.Polynomial object at 0x7f2d419ec390&gt; == &lt;w4_polynomial.Polynomial object at 0x7f2d419ec358&gt;
</code></pre>

<p>And now the Polynomial Class:</p>

<pre><code>class Polynomial():

def __init__(self, value=[0]):
    self.v = []
    self.n = []
    temp = list(reversed(value[:]))
    if value == [0]:
        self[0] = 0
    else:
        for x in range(0, len(temp)):
            self[x] = temp[x]
    #self.__compress()

...

def __eq__(self, value):
    temp1 = self.v[:]
    temp2 = self.n[:]
    temp3 = value.v[:]
    temp4 = value.n[:]
    temp1.sort()
    temp2.sort()
    temp3.sort()
    temp4.sort()
    return (temp1 == temp3 and temp2 == temp4)

def __ne__(self, value):
    temp1 = self.v[:]
    temp2 = self.n[:]
    temp3 = value.v[:]
    temp4 = value.n[:]
    temp1.sort()
    temp2.sort()
    temp3.sort()
    temp4.sort()
    return (temp1 != temp3 and temp2 != temp4)

...

def main():
    pass

if __name__=="__main__":
    main()
</code></pre>

<p>I think the issue is with the implementation with <code>def __ne__</code> function in Polynomial class.
assertNotEqual when called, expects a True value when the values passed are not equal. But in this class, you are directly sending the output of temp1 != temp3 and temp2 != temp4.</p>

<p>So, the function should be like this </p>

<blockquote>
<pre><code>def __ne__(self, value):
    temp1 = self.v[:]
    temp2 = self.n[:]
    temp3 = value.v[:]
    temp4 = value.n[:]
    temp1.sort()
    temp2.sort()
    temp3.sort()
    temp4.sort()
    result = True if not (temp1 != temp3 and temp2 != temp4) else False
    return result
</code></pre>
</blockquote>

python unit-testing python-3.x python-unittest