如何仅通过Feature名称运行Behave的多个Feature文件?
Got it, let's sort this out so you can run multiple feature files without typing those tedious full paths every time! Here are a few practical, easy-to-implement solutions:
1. Use Behave's Default Feature Directory
Behave automatically looks for feature files in a directory named features/ (in your current working directory) by default. If you organize your feature files here, you can run them directly by name:
- To run specific feature files with their
.featureextension:behave feature_name_1.feature feature_name_2.feature - If there’s no risk of name conflicts (no other files with the same name in the directory), you can even omit the
.featureextension:behave feature_name_1 feature_name_2
2. Set the BEHAVE_FEATURES Environment Variable
If your feature files are stored in a different directory (not the default features/), you can point Behave to that directory permanently using an environment variable:
For Linux/macOS (bash/zsh):
export BEHAVE_FEATURES="/absolute/path/to/your/feature/directory"
Add this line to your ~/.bashrc or ~/.zshrc to make it persist across terminal sessions.
For Windows (Command Prompt):
set BEHAVE_FEATURES=C:\absolute\path\to\your\feature\directory
To make this permanent, add the variable via System Properties → Environment Variables.
For Windows (PowerShell):
$env:BEHAVE_FEATURES = "C:\absolute\path\to\your\feature\directory"
Add this to your PowerShell profile to keep it persistent.
Once set, you can run multiple features from anywhere using just their names:
behave feature_name_1 feature_name_2
3. Specify the Feature Directory in the Command
If you don’t want to set an environment variable, you can explicitly tell Behave where to look for features using the --features flag, followed by the directory path and your feature names:
behave --features /path/to/your/features feature_name_1.feature feature_name_2.feature
4. Create a Simple Script (For Repeated Runs)
If you regularly run the same set of features, save yourself the hassle by creating a small script:
Linux/macOS (Shell Script):
Create a file named run_my_features.sh:
#!/bin/bash behave /full/path/to/feature_name_1.feature /full/path/to/feature_name_2.feature
Make it executable with chmod +x run_my_features.sh, then run it anytime with:
./run_my_features.sh
Windows (Batch Script):
Create a file named run_my_features.bat:
@echo off behave C:\full\path\to\feature_name_1.feature C:\full\path\to\feature_name_2.feature
Double-click the file or run it from Command Prompt.
内容的提问来源于stack exchange,提问作者Iwona

