conversion from Unittest Python2 to Python3

0

I'm having some free time and I'm upgrading to Python3.

I came across a problem in running the unittest, and I believe you can help me.

Error:

File "/home/brito/projetos/00-incolumepy/tests/generators_test.py", line 28
    self.assert_([(next(f) for x in xrange(6)] == [1, 1, 2, 6, 24, 120])
                                             ^
SyntaxError: invalid syntax

Follow the code:

import unittest
from platform import python_version
from incolumepy.utils.utils import fatorial
from incolumepy.utils.utils import gen_fibonacci

if python_version()>= '3.0':
    xrange = range


class UtilsTest(unittest.TestCase):
    def test_generator_fatorial1(self):
        f = fatorial() 
        self.assert_(next(f) == 1)

    def test_generator_fatorial2(self):
        f = fatorial()
        for x in xrange(4): next(f)
        self.assert_(next(f) == 24)

    def test_generator_fatorial3(self):
        f = fatorial()
        for x in xrange(6): next(f)
        self.assert_((next(f) == 720))

    def test_generator_fatorial4(self):
        f = fatorial()
        self.assert_([(next(f) for x in xrange(6)] == [1, 1, 2, 6, 24, 120])

    def test_generator_fatorial5(self):
        f = fatorial()
        self.assert_([(next(f) for x in xrange(10)] == \
            [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880])

    def test_generator_fibonacci1(self):
        f = gen_fibonacci()
        self.assert_(f.next() == 1)

    def test_generator_fibonacci2(self):
        f = gen_fibonacci()
        for x in xrange(4): f.next()
        self.assert_(f.next() == 5)

    def test_generator_fibonacci3(self):
        f = gen_fibonacci()
        for x in xrange(6): f.next()
        self.assert_(f.next() == 13)
        pass

    def test_generator_fibonacci4(self):
        f = gen_fibonacci()
        self.assert_([next(f) for x in xrange(6)] == [1, 1, 2, 3, 5, 8])

    def test_generator_fibonacci5(self):
        f = gen_fibonacci()
        self.assert_([next(f) for x in xrange(10)] == \
            [1, 1, 2, 3, 5, 8, 13, 21, 34, 55])


if __name__ == '__main__':
    unittest.main()

The following is the code for the imported modules:

def gen_fibonacci():
    '''
    Gerador para o proximo elemento da sequencia fibonacci
    '''
    a,b = 0, 1
    while 1:
        yield b
        a, b = b, a + b


def fatorial():
    '''
    Gerador para sequencia fatorial onde o primeiro elemento é zero
    '''
    a = 0
    while 1:
        yield math.factorial(a)
        a+=1
    
asked by anonymous 10.07.2017 / 14:09

0 answers