1

您好需要一些有关如何修复错误消息的帮助

E       fixture 'test_login' not found

>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, metadata, monkeypatch, pytestbdd_given_User logged into application, pytestbdd_given_trace, pytestbdd_then_trace, pytestbdd_when_trace, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, trace
>       use 'pytest --fixtures [testpath]' for help on them.

这是我的test_login.py

from pytest_bdd import scenario, given, when, then
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import pytest
import os



@scenario('../features/test.feature', 'Login scene')
def test_add():
    pass

class TestLogin:
    host = 'http://localhost:4723/wd/hub'
    desired_capabilities={
        'platformName': 'Android',
        'deviceName': 'Galaxy J7 Pro',
        'platformVersion': '9',
        'appPackage': 'com.beaheromobile',
        'appActivity': 'com.beaheromobile.MainActivity',
        'udid': '52002b7fb8eb8489',
        'adbExecTimeout': 120000,
    }
    credentials={
        'email': 'shane08@silva.com',
        'password': '123456'
    }

    driver = webdriver.Remote(host, desired_capabilities)
    action = TouchAction(driver)

    @given('User logged into application')
    def test_login(self):
        self.driver
        self.driver.implicitly_wait(8000)
        loginbtn = self.driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.TextView')
        self.action.tap(loginbtn).perform()
        self.driver.implicitly_wait(3000)

这是我的test.feature

Feature: Login
    A test login

Scenario: Login scene
    Given User logged into application
4

3 回答 3

0

您是否创建了功能文件,在这种情况下,您必须构建您的项目。

首先,您的测试类必须与您要测试的代码分开

https://github.com/AndyLPK247/tau-pytest-bdd/tree/chapter-4/tests

看看这个 repo 这就是你必须如何构建你的项目

功能文件 cucumber.feature

 Feature: Cucumber Basket
  As a gardener,
  I want to carry cucumbers in a basket,
  So that I don't drop them all.


  Scenario: Add cucumbers to a basket
    Given the basket has "2" cucumbers
    When "4" cucumbers are added to the basket
    Then the basket contains "6" cucumbers

然后你 test_steps_def.py

from pytest_bdd import scenario, parsers, given, when, then
from cucumbers import CucumberBasket


@scenario('../features/cucumber.feature', 'Add cucumbers to a basket')
def test_add():
    pass


@scenario('../features/cucumber.feature', 'Remove cucumbers to a basket')
def test_remove():
    pass

@given(parsers.cfparse('the basket has "{initial:Number}" cucumbers', extra_types=dict(Number=int)))
def basket(initial):
    return CucumberBasket(initial_count=initial)


@when(parsers.cfparse('"{some:Number}" cucumbers are added to the basket', extra_types=dict(Number=int)))
def add_cucumbers(basket, some):
    basket.add(some)


@when(parsers.cfparse('"{some:Number}" cucumbers are removed from the basket', extra_types=dict(Number=int)))
def remove_cucumbers(basket, some):
    basket.remove(some)


@then(parsers.cfparse('the basket contains "{total:Number}" cucumbers', extra_types=dict(Number=int)))
def basket_has_total(basket, total):
    assert basket.count == total
于 2020-06-18T03:48:50.090 回答
0

根据文档target_fixture,我们应该在@given装饰器中指定一个关键字参数。

因此,您的新定义应如下所示:

@given('User logged into application', target_fixture="test_login")
def test_login(author):
    pass
于 2020-09-28T14:21:00.557 回答
0

定义 test_login.py 文件的方式几乎没有问题。pytest_bdd 将查找与 test_add() 关联的场景,并将自动运行与场景关联的条件,例如给定、何时、然后等。这意味着 @given 条件的定义不应具有任何可识别的 pytest 方法名称,例如测试_*。否则,这会使 pytest_bdd 相信这是另一个测试用例,并将为其寻找相应的固定装置。这种方法对我有用。

所以请将 test_login() 方法重命名为 login1() 之类的其他名称,如下所示。这将确保测试工作正常。

from pytest_bdd import scenario, given, when, then
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
import pytest
import os



@scenario('../features/test.feature', 'Login scene')
def test_add():
    pass

class TestLogin:
    host = 'http://localhost:4723/wd/hub'
    desired_capabilities={
        'platformName': 'Android',
        'deviceName': 'Galaxy J7 Pro',
        'platformVersion': '9',
        'appPackage': 'com.beaheromobile',
        'appActivity': 'com.beaheromobile.MainActivity',
        'udid': '52002b7fb8eb8489',
        'adbExecTimeout': 120000,
    }
    credentials={
        'email': 'shane08@silva.com',
        'password': '123456'
    }

    driver = webdriver.Remote(host, desired_capabilities)
    action = TouchAction(driver)

@given('User logged into application')
def login1(self):
    self.driver
    self.driver.implicitly_wait(8000)
    loginbtn = self.driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[3]/android.widget.TextView')
    self.action.tap(loginbtn).perform()
    self.driver.implicitly_wait(3000)
于 2020-05-22T11:40:18.540 回答