pytest
允许您在某些条件下(即:在平台上)跳过测试:
@pytest.mark.skipif(sys.platform == "win32")
def test_foo(fixture):
assert bar(fixture)
是否可以创建标记以在特定条件下使测试失败?
@pytest.mark.failif(sys.platform == "win32")
def test_foo(fixture):
assert bar(fixture)
答案 0 :(得分:2)
即使没有pytest,您也可以编写一个简单的装饰器来做到这一点:
cap = cv2.VideoCapture(0)
prev_move = None
while True:
ret, frame = cap.read()
if not ret:
continue
# rectangle for user to play
cv2.rectangle(frame, (100, 100), (500, 500), (255, 255, 255), 2)
# rectangle for computer to play
cv2.rectangle(frame, (800, 100), (1200, 500), (255, 255, 255), 2)
# extract the region of image within the user rectangle
roi = frame[100:500, 100:500]
img = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (227, 227))
# predict the move made
pred = model.predict(np.array([img]))
move_code = np.argmax(pred[0])
user_move_name = mapper(move_code)
# predict the winner (human vs computer)
if prev_move != user_move_name:
if user_move_name != "none":
computer_move_name = choice(['rock', 'paper', 'scissors'])
winner = calculate_winner(user_move_name, computer_move_name)
else:
computer_move_name = "none"
winner = "Waiting..."
prev_move = user_move_name
# display the information
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "Your Move: " + user_move_name,
(50, 50), font, 1.2, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "Computer's Move: " + computer_move_name,
(750, 50), font, 1.2, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "Winner: " + winner,
(400, 600), font, 2, (0, 0, 255), 4, cv2.LINE_AA)
if computer_move_name != "none":
icon = cv2.imread(
"{}.png".format(computer_move_name))
icon = cv2.resize(icon, (400, 400))
frame[100:500, 800:1200] = icon
cv2.imshow("Rock Paper Scissors", frame)
k = cv2.waitKey(10)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
用法是
def failif(condition: bool, *, reason: str) -> Callable[[T], T]:
def failif_decorator(func: T) -> T:
@functools.wraps(func)
def failif_decorator_inner(*args: Any, **kwargs: Any) -> Any:
assert not condition, reason
return func(*args, **kwargs)
return cast(T, failif_decorator_inner)
return failif_decorator
对于特定于pytest的解决方案,您可以使用hooks for registering marks之一-尽管要花很多时间