Error running Python test 3.6.7

Asked

Viewed 85 times

1

This gives error when running def test() to test def questao(). Why would be giving error in the return of the test function?

def questao():
    lista_nomes = []
    nome = ''       
    x = 0    
    reader = csv.DictReader(arquivo)

    for linha in reader:
        nome = linha['full_name']        
        lista_nomes.append(nome)
        x += 1

        if  x == 20:
            break

    return lista_nomes

Testing

def teste():
    r = questao()
    assert (
        isinstance(r, list) and
        all(isinstance(y, str) for y in r)
    )

Error below:

C:\Users\perei\devpy\python-1\teste.py:25: AssertionError: assert [] == ['C. Ronaldo'..., 'Robert Pererira', ...]
  • By the looks of it you have two types of test, the first to know if r is a list and another to know if each item on the list has strings. I would sort out these tests, something like assert isinstance(r, list) == True and assert all(isinstance(y, str) for y in r) == True because they are different validations.

1 answer

0


Are you sure this test failure is from this test function? Why doesn’t she compare this - and the assert error message actually shows a list populated with the names read - which should be the correct return of the function questao.

You’re using pytest, right? (pytest modifies the assert Python to give error messages with the values compared - the assert alone would not show the two values being compared).

In the complete output you have in the terminal you must have the name and the spot of the test function that is failing - possibly is another.

If not, please update your question with the module code of your test function -including imports. And confirm that you are using pytest or another third-party library to run the tests.

  • Sorry, but I don’t have access to the test file. There is a difference whether or not to use virtualenv to do the tests?

  • There is no difference in whether or not to use virtualenv for testing.

Browser other questions tagged

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