Problem with Pytest

Asked

Viewed 119 times

0

I am trying to apply pytest in a project that uses Bhaskara’s Formula, because it is giving error and I am not able to solve. The following will be the codes with the error. Thank you.

import math

class Bhaskara:

    def delta(self, a, b, c):
        return b ** 2 - 4 * a * c

    def calcula_raizes(self, a, b, c):
        d = self.delta(a, b, c)
        if d == 0:
            raiz1 = (-b + math.sqrt(d)) / (2 * a)
            return 1, raiz1
        else:
            if d < 0:
                return 0
            else:
                raiz1 = (-b + math.sqrt(d)) / (2 * a)
                raiz2 = (-b - math.sqrt(d)) / (2 * a)
                return 2, raiz1, raiz2

import Bhaskara

import pytest

class Test_Bhaskara:

    @pytest.fixture
    def b(self):
        return Bhaskara.Bhaskara()

    @pytest.mark.parametrize("entrada, saida", [
            ((1, 0, 0), (1, 0,)),
            ((1, -5, 6), (2, 3, 2)),
            ((10, 10, 10), (0,)),
            ((10, 20, 10), (1, -1))
        ])

    def testa_bhaskara(self, entrada, saida):
        b = Bhaskara()
        assert b.calcula_raizes(entrada) == saida

Exit from the pytest

___ Test_Bhaskara.testa_bhaskara[entrada3-saida3] ___

self = <Test_Bhaskara.Test_Bhaskara instance at 0x7f1ea0453680>
entrada = (10, 20, 10), saida = (1, -1)

@pytest.mark.parametrize("entrada, saida", [
            [(1, 0, 0,), (1, 0,)],
            [(1, -5, 6,), (2, 3, 2,)],
            [(10, 10, 10,), (0,)],
            [(10, 20, 10,), (1, -1,)]
        ])

def testa_bhaskara(self, entrada, saida):
    b = Bhaskara()
    TypeError: 'module' object is not callable

Test_Bhaskara.py:14: TypeError
========= 4 failed in 0.04 seconds =========

1 answer

0

Use:

from Bhaskara import Bhaskara

Then he will carry the class "Bhaskara" inside "Bhaskara.py".

By the way, the calcul_roots() method is wrong because it waits three numerical values and you are sending one tuple for him. Not to touch what you’ve already mounted for the tests, try something like this:

def calcula_raizes(self, valores):
    (a, b, c) = valores
    d = self.delta(a, b, c)
    ...

That should resolve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.