Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing test data in Python [duplicate]

How can I run the same test against a lot of different data?

I want to be reported of all failures.

For example:

def isEven(number):
    return True # Quite buggy implementation

data = [
    (2, True),
    (3, False),
    (4, True),
    (5, False),
]

class MyTest:
   def evenTest(self, num, expected):
       self.assertEquals(expected, isEven(num))

I have found solution which raises error on first failure only: PHPUnit style dataProvider in Python unit test

How can I run a test to be reported of all failures?

like image 892
jinowolski Avatar asked Jun 14 '11 21:06

jinowolski


People also ask

How does Python handle duplicate data?

Pandas drop_duplicates() method helps in removing duplicates from the Pandas Dataframe In Python.

How do you duplicate a dataset in Python?

Pandas DataFrame duplicated() Method The duplicated() method returns a Series with True and False values that describe which rows in the DataFrame are duplicated and not. Use the subset parameter to specify if any columns should not be considered when looking for duplicates.

Which allows duplicate values in Python?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.


1 Answers

If you are using pytest you can go this way:

import pytest

def is_even(number):
    return True # Wuite buggy implementation

@pytest.mark.parametrize("number, expected", [
    (2, True),
    (3, False),
    (4, True),
    (5, False)
])
def test_is_even(number, expected):
    assert is_even(number) == expected

You will get something like (shortened):

/tmp/test_it.py:13: AssertionError
=========== 2 failed, 2 passed in 0.01 seconds ====================
like image 197
modesto Avatar answered Oct 16 '22 17:10

modesto