Python 中的 assertEquals 到底是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17920625/
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 09:29:19  来源:igfitidea点击:

What is actually assertEquals in Python?

pythondjangodjango-tests

提问by Rockhound

I have the following test.py file in django. can you please explain this code?

我在 Django 中有以下 test.py 文件。你能解释一下这段代码吗?

from contacts.models import Contact
...
class ContactTests(TestCase):
    """Contact model tests."""

    def test_str(self):

        contact = Contact(first_name='John', last_name='Smith')

        self.assertEquals(
            str(contact),
            'John Smith',
        )

采纳答案by Hieu Nguyen

from contacts.models import Contact  # import model Contact
...
class ContactTests(TestCase):  # start a test case
    """Contact model tests."""

    def test_str(self):  # start one test

        contact = Contact(first_name='John', last_name='Smith')  # create a Contact object with 2 params like that

        self.assertEquals(  # check if str(contact) == 'John Smith'
            str(contact),  
            'John Smith',
        )

Basically it will check if str(contact) == 'John Smith', if not then assert equal is failed and the test is failed and it will notify you the error at that line.

基本上它会检查 str(contact) == 'John Smith',如果不是,则 assert equal 失败并且测试失败,它会在该行通知您错误。

In other words, assertEquals is a function to check if two variables are equal, for purposes of automated testing:

换句话说, assertEquals 是一个用于检查两个变量是否相等的函数,用于自动化测试:

def assertEquals(var1, var2):
    if var1 == var2:
        return True
    else:
        return False

Hope it helps.

希望能帮助到你。

回答by Maxime Lorant

The assertEquals set your test as passed if the __str__of your contactobject returns 'John Smith`. This is part of unit tests, you should check the official documentation

如果__str__您的contact对象返回“John Smith”,则 assertEquals 将您的测试设置为通过。这是单元测试的一部分,您应该查看官方文档

回答by Martijn Pieters

assertEqualsis a (deprecated) alias for TestCase.assertEqual, which is a method on the unittest.TestCaseclass.

assertEquals是 的(不推荐使用的)别名TestCase.assertEqual,它是上的一个方法unittest.TestCase

It forms a test assertion; where str(contact)must be equal to 'John Smith'for the test to pass.

它形成一个测试断言;wherestr(contact)必须等于'John Smith'测试才能通过。

The form with shas been marked as deprecated since 2010, but they've not actually been removed, and there is no concrete commitment to remove them at this point. If you run your tests with deprecation warnings enabled (as recommended in PEP 565) you'd see a warning:

自 2010 年以来,带有 的表单s已被标记为已弃用,但实际上并未将其删除,此时也没有具体的承诺来删除它们。如果您在启用弃用警告的情况下运行测试(如PEP 565 中推荐的那样),您会看到警告:

test.py:42: DeprecationWarning: Please use assertEqual instead.
  self.assertEquals(

回答by Ami Patel

Syntax: assertEqual(first, second, msg=None)

句法: assertEqual(first, second, msg=None)

Test that first and second are equal. If the values do not compare equal, the test will fail.In addition it will also check if first and second are the exact same type and one of list, tuple, dict, set, frozenset or unicode.

测试 first 和 second 是否相等。如果比较的值不相等,则测试将失败。此外,它还将检查 first 和 second 是否完全相同,以及 list、tuple、dict、set、frozenset 或 unicode 中的一个。

in your case it will check will check if str(contact) == 'John Smith', if not then assert equal is failed.

在您的情况下,它将检查将检查if str(contact) == 'John Smith',如果不是,则断言相等失败。

回答by Philpot

assertEquals tests whether two variables are equal to each other.

assertEquals 测试两个变量是否彼此相等。

回答by Boris

assertEqualsis deprecated since Python 3.2, you should use assertEqual(no s).

assertEquals自 Python 3.2 起已弃用,您应该使用assertEqual(no s)。

Or pytest.

或者pytest