Python unittest data provider
Asked Answered
B

2

14

I am trying to create a unit test in python that has a data provider. As the unittest library does not support this nativity, I'm using the unittest_data_provider package. I'm getting an error, and am not sure where it is coming from (I'm new to python).

My code

import unittest
from wikibase.dataModel.item_id import ItemId
from unittest_data_provider import data_provider


class TestItemId(unittest.TestCase):
    itemIds = lambda: (
        ( 'q42' ),
        ( 'Q42' ),
        ( 'Q1' ),
        ( 'Q1000' ),
        ( 'Q31337' ),
    )

    @data_provider(itemIds)
    def test_constructor(self, itemString):
        itemId = ItemId(itemString)
        self.assertEqual(itemId.getSerialization(), itemString)

The error I get:

File "/usr/local/lib/python3.3/dist-packages/unittest_data_provider/init.py", line 7, in repl fn(self, *i) TypeError: test_constructor() takes 2 positional arguments but 4 were given

This is using python 3.3.

Barbital answered 19/9, 2013 at 21:59 Comment(1)
The github page of unittest-data-provider states: !!! Don't use this, try nose test generators instead !!! See this urlAcetylcholine
P
12

Your itemIds function should return a tuple of tuples, but the way you have coded it, it is returning a tuple of strings. You need to add a , inside the parenthesis to return a single item tuple, try replacing your code with the following:

itemIds = lambda: (('q42',), ('Q42',), ('Q1', ), ('Q1000',), ('Q31337',),)
Premium answered 19/9, 2013 at 22:43 Comment(0)
F
0

Jeroen De Dauw Decorator DataProvider can only Iterate over callable objects i.e Tuples, lists, set, int etc but Not on 2D , you are passing Tuple of Tuple and Decorator data provider is not implemented to handle it.

Hence you can go for idata decorator of ddt. It provides the ans to your requirement

Happy Coding

Fouts answered 20/11, 2018 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.