Python新手求助:Pydev中Behave安装后运行异常问题
Hey there! Since you confirmed the terminal behave command works perfectly, your core setup (Python, Behave installation, test code) is solid—we just need to tweak a few IDE configurations to get everything running smoothly in Eclipse. Let's break down each problem:
1. "Undefined variables given, when, then" Error
This is a PyDev code analysis quirk—PyDev doesn't recognize that given, when, then are global decorators provided by Behave. Here are two easy fixes:
- Quick fix per step file: Add this comment at the top of your
step.pyfile to tell PyDev to ignore these undefined variables:#@PydevCodeAnalysisIgnore - Global fix for all projects: Go to
Window > Preferences > PyDev > Editor > Code Analysis > Undefined, then:- Under Forced Builtins, add
behave(this tells PyDev to recognize Behave's global symbols) - Or, under Ignore locally, add
given,when,thento exclude these specific names from the undefined check
- Under Forced Builtins, add
2. 0 Tests Executed When Running step.py as Python Unit Test
Behave step files aren't standard Python unit tests (they don't use unittest.TestCase or pytest-style assertions directly). You can't run them as unit tests—instead, you need to run them via Behave's dedicated runner:
- Right-click your
.featurefile (or the entire test directory) - Select
Run As > Run Configurations - Create a new Behave configuration:
- Name it something descriptive (e.g., "My Behave Tests")
- Under Main, specify the path to your
.featurefile or test directory - Click Run—this will execute all your Behave tests properly, just like the terminal command
3. "No matching glue code" Warning in .feature Files
The Cucumber plugin can't locate your step definitions because it doesn't know where to look for them. Fix this by setting the glue path:
- Right-click your project >
Properties > Cucumber - In the Glue section, add the path to the directory/package where your
step.pylives (e.g.,features/stepsor your Python package name) - Make sure your
.featurefiles are in the defaultfeaturesdirectory (or adjust the Behave run configuration to point to their location if you're using a custom structure)
Once you apply these changes, your IDE should play nice with Behave, and all those annoying warnings/errors should disappear. Happy testing!
内容的提问来源于stack exchange,提问作者Benjamin Bettan

