Skip to main content
 首页 » 编程设计

python之Pycharm 没有发现使用 pytest、pytest bdd 的测试

2024年10月01日1298°冷暖

我有一个问题:我正在尝试使用 pytest-bdd 创建一个测试,但我收到一个错误:

platform darwin -- Python 3.5.2, pytest-3.0.7, py-1.4.32, pluggy-0.4.0 
rootdir: /Users/harmatii/PycharmProjects/guru99/bdd, inifile: 
plugins: bdd-2.18.2 
collected 0 items / 1 errors 
 
No tests found 

这是我来自 2 个文件的代码:

Scenario Outline: Add New Customer 
  Given a customer form 
  When we fill out form with customer information <name>, <email>, <password> 
  Then verify that page redirects to details of added customer 
 
 
 Examples: 
 |name    | |email          | |password | 
 |Chris   | |hhh1@gmail.com | |password | 

和我的测试文件:

from pytest_bdd import scenario, given, when, then 
from methods.customer import * 
from methods.login import * 
from methods.is_present import * 
 
 
@scenario('customers.feature', 'Add New Customer') 
def test_customers(): 
    pass 
 
 
@given('a customer form') 
def customer_form(driver): 
    login(driver, username="mngr91344", password="ubymYtA") 
 
 
@when('we fill out form with customer information <name>, <email>, <password>') 
def add_customer(driver): 
    add_new_customer(driver, name="Chris", email="hhh1@gmail.com", password="password") 
 
 
@then('verify that page redirects to details of added customer') 
def verify_page(driver): 
    is_element_present(driver, By.LINK_TEXT, "Customer Registered Successfully!!!") 

同一项目中的其他常规测试(没有 bdd)运行没有问题。

请您参考如下方法:

py.test 报告发现 1 个错误:

ValueError: In "parametrize" the number of values (['Chris', '', 'hhh1@gmail.com', '', 'password']) must be equal to the number of names (['name', 'email', 'password']) 

从 customers.feature 的示例部分中删除空列:

 Examples: 
 |name    |email          |password | 
 |Chris   |hhh1@gmail.com |password | 

然后测试采集成功:

$ py.test --collect-only 
=========================== test session starts =========================== 
platform linux -- Python 3.6.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 
rootdir: /home/they4kman/.virtualenvs/tmp-5ce0cdbde232a4e/src, inifile: 
plugins: bdd-2.18.2 
collected 1 item                                                            
<Module 'test_customer_steps.py'> 
  <Function 'test_customers[Chris-hhh1@gmail.com-password]'>