>>> assert 1 + 1 == 2
>>> assert isinstance('Hello', str)
>>> assert isinstance('Hello', int)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AssertionError
>>> s = "nothin is impossible."
>>> key = "nothing"
>>> assert key in s, "Key: '{}' is not in Target: '{}'".format(key, s)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AssertionError: Key: 'nothing' is not in Target: 'nothin is impossible.'
import pytest
def test_case():
expected = "Hello"
actual = "hello"
assert expected == actual
if __name__ == '__main__':
pytest.main()
"""
================================== FAILURES ===================================
__________________________________ test_case __________________________________
def test_case():
expected = "Hello"
actual = "hello"
> assert expected == actual
E assert 'Hello' == 'hello'
E - Hello
E ? ^
E + hello
E ? ^
assertion_in_python.py:7: AssertionError
========================== 1 failed in 0.05 seconds ===========================
""""
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FoO')
if __name__ == '__main__':
unittest.main()
"""
Failure
Expected :'FOO'
Actual :'FoO'
Traceback (most recent call last):
File "assertion_in_python.py", line 6, in test_upper
self.assertEqual('foo'.upper(), 'FoO')
AssertionError: 'FOO' != 'FoO'
"""
from ptest.decorator import *
from ptest.assertion import *
@TestClass()
class TestCases:
@Test()
def test1(self):
actual = 'foo'
expected = 'bar'
assert_that(expected).is_equal_to(actual)
"""
Start to run following 1 tests:
------------------------------
...
[demo.assertion_in_python.TestCases.test1@Test] Failed with following message:
...
AssertionError: Unexpectedly that the str <bar> is not equal to str <foo>.
"""
pip install assertpy
from assertpy import assert_that
def test_something():
assert_that(1 + 2).is_equal_to(3)
assert_that('foobar')\
.is_length(6)\
.starts_with('foo')\
.ends_with('bar')
assert_that(['a', 'b', 'c'])\
.contains('a')\
.does_not_contain('x')
Expected <foo> to be of length <4>, but was <3>. Expected <foo> to be empty string, but was not. Expected <False>, but was not. Expected <foo> to contain only digits, but did not. Expected <123> to contain only alphabetic chars, but did not. Expected <foo> to contain only uppercase chars, but did not. Expected <FOO> to contain only lowercase chars, but did not. Expected <foo> to be equal to <bar>, but was not. Expected <foo> to be not equal to <foo>, but was. Expected <foo> to be case-insensitive equal to <BAR>, but was not.
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有