Python Selenium Mocking - заглушка словаря в тесте сбоя mock с помощью AttributeError, почему? - PullRequest
0 голосов
/ 07 ноября 2018

У меня проблема с заглушкой словаря внутри макета, и мой тест вылетает. Разве макет не должен заменить все для начала, поэтому я даже не должен сталкиваться с этой проблемой?

script.py

from selenium import webdriver

def nothing_here_really_just_setting_up_the_logic_flow():
   """
   This function is completely irrelevant. It just loads a website
   returns the webdriver session as "driver" and a dictionary 
   "add extra xpath" with the addtional xpath syntax for the function
   where my issue is at
   """     

    # Load a website
    driver = webdriver.Chrome()
    driver.get(#some website)

    # Prepare extra xpath syntax
    add_extra_xpath = {}
    add_extra_xpath['add me'] = 1 # The actual dict is more complex then this and 
                                  # this number keeps changing so that's why I'm using a 
                                  # dict, if you're wondering

    return driver, add_extra_xpath


def function_where_I_have_a_problem(driver, add_extra_xpath):
    """
    Simply find an element on the site via a connotated xpath
    """

    # Find an element by xpath with the help of "add_extra_path"
    find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')

test_script.py

import pytest
import script

from unittest.mock import MagicMock

# Mock the "driver" and stub the "add_extra_xpath" dictionary
@pytest.fixture
def fixture():

    driver = MagicMock()
    driver.find_element_by_xpath.side_effect = driver

    add_extra_xpath = {}
    add_extra_xpath['add me'] = 1

    return driver, add_extra_xpath

def test_function_where_I_have_a_problem(fixture):

    driver, add_extra_xpath = fixture

    script.function_where_I_have_a_problem(driver, add_extra_xpath)

    assert driver.call_count == 1

Запуск теста дает сбой из-за:

AttributeError: у объекта 'dict' нет атрибута 'find_element_by_xpath'

в

find = driver.find_element_by_xpath('begin writing xpath' + str(add_extra_xpath['add me']) + 'finish off the xpath')

Не должен ли побочный эффект, который я установил обратно в своем приборе, переопределить этот 'find_element_by_xpath'?

...