请检查以下代码:
import unittest
CORRECT_MESSAGE = 'Correct message'
WRONG_MESSAGE = 'Wrong message'
def fn():
raise KeyError(CORRECT_MESSAGE)
class Test(unittest.TestCase):
def test(self):
# I am expecting this test to fail as the msg I am
# checking is WRONG_MESSAGE, and not CORRECT_MESSAGE.
with self.assertRaises(KeyError, msg=WRONG_MESSAGE):
fn()
unittest.main()
Run Code Online (Sandbox Code Playgroud)
正如评论中提到的,我预计此测试会失败,因为我正在检查的消息 ( WRONG_MESSAGE) 不正确,但测试通过了。
我缺少什么?我已经检查过:assertRaises(exception, *, msg=None)。
int a = 5 , b = 2 ;
double ans1 = a / b ; // ans1 = 2
cout << setprecision ( 4 ) << fixed << ans1 << endl ;
unsigned short int c = USHRT_MAX , d = USHRT_MAX ;
unsigned int ans2 = c + d ; // ans2 = 131070
cout << ans2 ;
Run Code Online (Sandbox Code Playgroud)
评估(a/b)时会发生什么?
1)结果首先存储在int(RHS上的变量类型)然后转换为64位double(LHS上的变量类型)然后存储在ans1或第一个变量中转换为64位double(变量类型on LHS)然后/发生?
2)如果首先在RHS上评估为变量类型,那么第二个如何打印正确的ans(因为ans超出了unsigned short int的限制)
我有一个清单。我想从中处理列表中的项目variable starting point。
lines = ["line 0", ..., "line 25", ...]
cursor = 25
for i, line in enumerate(lines, start=cursor):
print("cursor is at:", cursor)
print("start line is:", lines[cursor])
print("actual line is:", line)
Run Code Online (Sandbox Code Playgroud)
输出:
cursor is at: 25
start line is: line 25
actual line is: line 0
...
Run Code Online (Sandbox Code Playgroud)
我希望enumerate从cursor这里开始,但是它从开始0。
我敢肯定我误解了一些东西,但是我真的很想了解如何enumerate改进我的Python。
请检查以下代码:
import unittest
def fn():
raise KeyError('my message')
class Test(unittest.TestCase):
def test_passes(self):
with self.assertRaisesRegex(KeyError, 'my message'):
fn()
with self.assertRaisesRegex(KeyError, 'my'):
fn()
def test_fails(self):
# Why does this test fail?
# Isn't '^my message$' a valid regex?
# How do I check if the message is exactly 'my message'?
with self.assertRaisesRegex(KeyError, '^my message$'):
fn()
unittest.main()
Run Code Online (Sandbox Code Playgroud)
在输出中,我收到以下测试消息test_fails:
AssertionError:“^我的消息$”与“'我的消息'”不匹配
我缺少什么?
正如评论中提到的,这些是我的问题:
'^my message$'有效的正则表达式吗?exactly 'my message'?