检查实例类型的Python测试
我想在python中使用unittest检查方法是否返回正确类的对象 web中的每个示例都显示了对返回的“type”的测试 例如,要检查检查实例类型的Python测试,python,testing,Python,Testing,我想在python中使用unittest检查方法是否返回正确类的对象 web中的每个示例都显示了对返回的“type”的测试 例如,要检查或,我们可以使用: self.assertIsInstance(result, list) self.assertIsInstance(result[0], tuple) 我要找的是一个检查 非常感谢您的帮助。谢谢。这应该可以: self.assertIsInstance(result, sqlalchemy.orm.query.Query) 您需要在文件
或
,我们可以使用:
self.assertIsInstance(result, list)
self.assertIsInstance(result[0], tuple)
我要找的是一个检查
非常感谢您的帮助。谢谢。这应该可以:
self.assertIsInstance(result, sqlalchemy.orm.query.Query)
您需要在文件中导入sqlalchemy。您可以使用
assertIsInstance()
,这可能是测试类型的推荐函数。根据上下文,您还可以将assertIs()
或assertTrue()
与type()
组合使用:
#assert.py
import unittest
class TestType(unittest.TestCase):
def setUp(self):
self.number = 1
def test_assert_true(self):
self.assertTrue(type(self.number) is int)
def test_assert_is_instance(self):
self.assertIsInstance(self.number, int)
def test_assert_is_with_type(self):
self.assertIs(type(self.number), int)
def test_assert_is(self):
self.assertIs(self.number, int)
if __name__ == '__main__':
unittest.main()
$ python assert.py
test_assert_is (__main__.TestType) ... FAIL
test_assert_is_instance (__main__.TestType) ... ok
test_assert_is_with_type (__main__.TestType) ... ok
test_assert_true (__main__.TestType) ... ok
======================================================================
FAIL: test_assert_is (__main__.TestType)
----------------------------------------------------------------------
Traceback (most recent call last):
File "assert.py", line 19, in test_assert_is
self.assertIs(self.number, int)
AssertionError: 1 is not <type 'int'>
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures=1)
#assert.py
导入单元测试
类TestType(unittest.TestCase):
def设置(自):
self.number=1
def test_assert_true(自我):
self.assertTrue(类型(self.number)为int)
def test_assert_是_实例(自身):
self.assertIsInstance(self.number,int)
def test_assert_为_类型(自身):
断言(类型(self.number),int)
def测试断言是(自身):
断言(self.number,int)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
unittest.main()
$python assert.py
test_assert_是(uuu main_uuu.TestType)。。。失败
test\u assert\u是\u实例(\uuuu main\uuuu.TestType)。。。好啊
test_assert_是带有_类型(uu main_uu.TestType)。。。好啊
test\u assert\u true(\uuuuu main\uuuuuu.TestType)。。。好啊
======================================================================
失败:test\u assert\u is(\uuuuu main\uuuuu.TestType)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
test_assert_中第19行的文件“assert.py”为
断言(self.number,int)
断言错误:1不是
----------------------------------------------------------------------
在0.000秒内运行了4次测试
失败(失败=1)
test\u assert\u is(self)
的断言错误可能会让人相信1的类型不是整数,但是它会比较1表示的对象和描述整数类型的对象。这很可能就是为什么首选isinstance()
,因为它对所检查的内容更详细,所涉及的键入更少,所以通常不太容易出错。…那么,问题出在哪里呢?只需将查询类传递给isinstance
。我认为isinstance
不是正确的使用方法。相反,assertIs
工作正常。我忘了使用import语句。谢谢你。然而,我认为assertIsInstance
不是正确的测试方法。它返回此错误:AssertionError:不是的实例。完全没有意义,但是@Brock Hargreaves的方法成功了。assertions
成功了!!。谢谢。@BrockHargreaves三个独立的测试结果可能更容易理解。(test\u assert\u true
,test\u assert\u是实例
,test\u assert\u是
)assertIsInstance
对我来说很有效