Python Behave测试用例pre-commit校验失败问题求助
Hey Jack, let's break down what's causing this error and how to fix it right away:
The Root Cause
You're calling .format(button_color="red") on your multi-line string, but there's no {button_color} placeholder anywhere in that string. Python expects to replace the placeholder with your value, but can't find where to put it—hence the error.
Solutions
We have two straightforward fixes depending on what you need:
1. If you actually need to use the button_color value
Add the {button_color} placeholder in the string where you want the color to appear. For example, if you want to note the button's color in your step definition:
context.execute_steps( """ Given user is on the landing page When he clicks the {button_color} LOGIN button And he provides the correct credential And clicks the login button Then he should be get into the main page """.format( button_color="red" ) )
Or if you want to emphasize it for clarity:
context.execute_steps( """ Given user is on the landing page When he clicks the *{button_color}* LOGIN button And he provides the correct credential And clicks the login button Then he should be get into the main page """.format( button_color="red" ) )
2. If the button_color argument is unnecessary
If you don't actually need to inject the color value into your steps, just remove the .format(button_color="red") call entirely:
context.execute_steps( """ Given user is on the landing page When he clicks the LOGIN button And he provides the correct credential And clicks the login button Then he should be get into the main page """ )
Bonus: Cleaner Formatting with Python 3.6+
If you're using Python 3.6 or newer, you can use f-strings for more readable code (especially if you need to add more variables later). Here's how that would look:
button_color = "red" context.execute_steps( f""" Given user is on the landing page When he clicks the *{button_color}* LOGIN button And he provides the correct credential And clicks the login button Then he should be get into the main page """ )
内容的提问来源于stack exchange,提问作者user3595231

