2

I want to test a function that parse data from a web page, but I don't want that my test depend of modification of the HTML code or internet so I save the web pages. The problem in the function I want to test I've got:

url_query = "http://www.allocine.fr/film/fichefilm-%s/similaire/" %allocine_id
response = requests.get(url_query)
soup = BeautifulSoup(response.text, "html.parser")

So I did in my test:

def test_Allocine_matrix_reloaded(self):
    #Load result Matrix allocine API
    test_file = open(MATRIX_RELOADED_TEST_FILE)
    matrix_reloaded_data = json.load(test_file)
    test_file.close()

    #Load result Matrix sim allocine webpage
    test_page = open(MATRIX_RELOADED_TEST_FILE)
    matrix_reloaded_sim_page = test_page.read()
    test_page.close()

    #Mocking functions
    allocine.get_allocine_info = mock.MagicMock(return_value=matrix_reloaded_data)
    requests.get = mock.MagicMock(return_value=matrix_reloaded_sim_page)

But I've got the error:

Traceback (most recent call last):
  File "info_allocine_test.py", line 34, in test_Allocine_matrix_reloaded
    friends_allocine_info = allocine.AllocineInfo(matrix_realoaded_allocine_id)
  File "info_allocine_flat.py", line 116, in __init__
    sim_allocine['sim'] = scrap_similar_movie_allocine(allocine_id)
  File "info_allocine_flat.py", line 255, in scrap_similar_movie_allocine
    soup = BeautifulSoup(response.text, "html.parser")
AttributeError: 'str' object has no attribute 'text'

How can I do to get the HTML code in soup variable? (I'm using unites and mock)

1 Answer 1

4

The best thing to do here would be to extract those three lines into a separate method or function which returns the downloaded text or the BeautifulSoup object. Your real function can call this to download the text, but your test can mock the whole function.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is still present except now I've got: AttributeError: 'str' object has no attribute 'find_all' when i'm trying to do so: sim_movie_links = soup.find_all("h2")
I don't know how I can help without seeing your updated code. But it seems likely you're returning a string, rather than a BS object, from your mocked method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.