gpt4 book ai didi

python - Unittest 采用 1 个位置参数,但给出了 2 个

转载 作者:行者123 更新时间:2023-12-05 05:45:36 27 4
gpt4 key购买 nike

我正在尝试对我的代码进行单元测试,但我总是收到以下错误:

错误:test_FlipVertically1D_empty_array(ma​​in.TestMarginSampling)

追溯(最近的调用最后):文件“C:\Users\s\anaconda3\lib\unittest\mock.py”,第 1337 行,已修补返回函数(*newargs,**newkeywargs)TypeError: test_FlipVertically1D_empty_array() 采用 1 个位置参数,但给出了 2 个

这是我类(class)的代码:

class FlipVertically1D:   
@staticmethod
def calc(data: np.ndarray) -> np.ndarray:
return - data

这里是我的单元测试代码:

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)

我首先假设我在某个地方过多地传递了 self 但这并没有解决我的问题

最佳答案

问题可能是 ddt 注释。通常,unittest 测试用例看起来与您的代码完全一样,除了 self 之外没有其他参数。

Example of unittest :

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()

但是对于 ddt,这发生了变化,他们期望第二个参数 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))

请注意,第一个示例 (test_undecorated) 看起来像您的代码,因此它看起来应该可以工作,但可能只是不工作并且无论如何都会转换方法。

关于python - Unittest 采用 1 个位置参数,但给出了 2 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71307334/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com