I'm trying to unittest my code however I always obtain the following error:
ERROR: test_FlipVertically1D_empty_array (main.TestMarginSampling)
Traceback (most recent call last): File "C:\Users\s\anaconda3\lib\unittest\mock.py", line 1337, in patched return func(*newargs, **newkeywargs) TypeError: test_FlipVertically1D_empty_array() takes 1 positional argument but 2 were given
Here is the code for my class:
class FlipVertically1D:
@staticmethod
def calc(data: np.ndarray) -> np.ndarray:
return - data
and here the code for my unittest:
import pandas as pd
import numpy as np
import unittest
from unittest.mock import patch, MagicMock
from ddt import ddt, data, file_data, idata, unpack
from augmentation.augment import FlipVertically1D as FlipV1D
from augmentation.augment import FlipHorizontally1D as FlipH1D
from augmentation.augment import RandomFlipping1D as RandF1D
@ddt
class TestMarginSampling1D(unittest.TestCase):
def set_up(self):
pass
def tear_down(self):
pass #del self.y_predict
def test_FlipVertically1D_empty_array(self):
empty = FlipV1D.calc(np.array([]))
self.assertTrue(len(empty))
#self.assertEqual(FlipV1D.calc(), 0)
I first assumed that somewhere I passed a self
too much but this didnt solve my problem
CodePudding user response:
The issue is probably the ddt
annotation. Normally, unittest test cases look exactly like your code, with no parameters other than self
.
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
But with ddt, this changes, they expect a second argument value
:
https://ddt.readthedocs.io/en/latest/example.html
@ddt
class FooTestCase(unittest.TestCase):
def test_undecorated(self):
self.assertTrue(larger_than_two(24))
@data(3, 4, 12, 23)
def test_larger_than_two(self, value):
self.assertTrue(larger_than_two(value))
Note that the first example (test_undecorated) looks like your code, so it seems like it's intended to work but maybe just doesn't and transforms the method anyway.