Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unittest data provider

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.

like image 759
Jeroen De Dauw Avatar asked Sep 19 '13 21:09

Jeroen De Dauw


People also ask

Can you run your own tests with Python unittest?

Moreover, we saw Python Unittest example and working. Also, we discussed Python Unit Testing frameworks and test case example with Python Unittest assert. We hope you can run your own tests for your code. In this tutorial, we saw how to do that with the Python Unittest and pytest modules.

What is unittest and how does it work?

Unittest supports skipping individual test methods and even whole classes of tests. In addition, it supports marking a test as an “expected failure,” a test that is broken and will fail, but shouldn’t be counted as a failure on a TestResult.

What is the Python equivalent of unittest discover?

As a shortcut, python -m unittest is the equivalent of python -m unittest discover. If you want to pass arguments to test discovery the discover sub-command must be used explicitly. The discover sub-command has the following options: Verbose output Directory to start discovery (. default) Pattern to match test files ( test*.py default)

How do I specify a test module in unittest?

Test modules can be specified by file path as well: python -m unittest tests/test_something.py This allows you to use the shell filename completion to specify the test module. The file specified must still be importable as a module.


1 Answers

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',),)
like image 168
Jaime Avatar answered Oct 14 '22 21:10

Jaime