在python

时间:2016-04-07 07:42:30

标签: python unit-testing mocking

我想测试一个解析网页数据的函数,但我不希望我的测试依赖于修改HTML代码或互联网,所以我保存了网页。我要测试的函数中的问题我得到了:

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

所以我在测试中做了:

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)

但我有错误:

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'

如何在soup变量中获取HTML代码? (我正在使用联合并模拟)

1 个答案:

答案 0 :(得分:2)

这里最好的做法是将这三行提取到一个单独的方法或函数中,该方法或函数返回下载的文本或BeautifulSoup对象。您的真实函数可以调用它来下载文本,但您的测试可以模拟整个函数。