用于检查实例类型的 Python 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33657463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:43:33  来源:igfitidea点击:

Python test to check instance type

pythontesting

提问by Bhavtosh

I want to use unittest in python to check if a method returns object of the right class.

我想在 python 中使用 unittest 来检查方法是否返回正确类的对象。

Every example in the web shows tests for 'type' returned.

网络中的每个示例都显示了对返回的“类型”的测试。

For example, to check for <type 'list'>or <type 'type'>, we could use:

例如,要检查 <type 'list'>or <type 'type'>,我们可以使用:

self.assertIsInstance(result, list)
self.assertIsInstance(result[0], tuple) 

What I am looking for is an example to check for <class'sqlalchemy.orm.query.Query'>

我正在寻找的是一个检查的例子 <class'sqlalchemy.orm.query.Query'>

Would appreciate any help. Thankyou.

将不胜感激任何帮助。谢谢你。

回答by Vivek Pandey

This should work:

这应该有效:

self.assertIsInstance(result, sqlalchemy.orm.query.Query)

You need to have import sqlalchemyin the file.

你需要import sqlalchemy在文件中。

回答by Brock Hargreaves

You could use assertIsInstance(), presumably using isinstance()which is the recommended function for testing types. You could also assertIs()or assertTrue()combined with type()depending on the context:

您可以使用assertIsInstance(),大概使用isinstance()which 是测试类型的推荐函数。根据上下文,您还可以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)

The assertion error of the test_assert_is(self)might lead one to believe the type of 1 is not integer however it's comparing the object represented by 1 and the object that describes the type of an integer. This is most likely why isinstance()is preferred since it's more verbose about what it's checking and less typing is involved, so in general less error prone.

的断言错误test_assert_is(self)可能会导致人们相信 1 的类型不是整数,但它正在比较 1 表示的对象和描述整数类型的对象。这很可能isinstance()是首选的原因,因为它更详细地说明要检查的内容并且涉及的输入更少,因此通常不易出错。