断言 inputList == inputListSorted

问题描述 投票:0回答:2

你能帮我吗,我的代码有什么问题吗?

def generalized_price_sorter_expensive_cheap_assert(inputList, typeOfSort):
    #print(inputList)
    if typeOfSort == "cheap":
        inputListSorted = inputList.copy()
        inputListSorted.sort()


        if inputList == inputListSorted:  ##compare first list to second list, if is equal = good
            print("Cheap sorter is OK")

        else:
            print("Cheap sorter is NOT OK")

    if typeOfSort == "expensive":
        inputListSorted = inputList.copy()
        inputListSorted.sort(reverse=True)
        if inputList == inputListSorted:
            print("Expensive sorter is OK")
        else:
            print("Expensive sorter is NOT OK")

    print("LIST FROM WEB:")
    print(inputList)
    print("CORRECTLY SORTED LIST")
    print(inputListSorted)

    assert inputList == inputListSorted

我的断言错误:

<selenium.webdriver.remote.webelement.WebElement (session="7d749d08e3af4a8efc0322859c6b3e2e", element="0D305CB722AA126A875927A62168E5F6_element_91")>
[14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938, 12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838, 11318, 11858, 11458, 10938]
Expensive sorter is NOT OK
LIST FROM WEB:
[14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938, 12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838, 11318, 11858, 11458, 10938]
CORRECTLY SORTED LIST
[14818, 13398, 12958, 12178, 12138, 12058, 12018, 11938, 11858, 11558, 11498, 11458, 11378, 11318, 11238, 11178, 11038, 10938, 10838, 10478]
python windows selenium-webdriver testing pycharm
2个回答
0
投票

您需要将排序后的列表分配给变量,因为排序未到位。

inputListSorted = inputList.copy()
inputListSorted = sorted(inputListSorted)

# Or in a single line

inputListSorted = sorted(inputList.copy())

0
投票

您收到断言错误,因为列表之间的元素顺序不同。如果您想检查两个列表是否具有相同的元素,您可以这样做:

import collections

def assert_same_elements(left, right):
    assert collections.Counter(left) == collections.Counter(right)

list_from_web = [
    14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938,
    12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838,
    11318, 11858, 11458, 10938
]

correctly_sorted_list = [
    14818, 13398, 12958, 12178, 12138, 12058, 12018, 11938,
    11858, 11558, 11498, 11458, 11378, 11318, 11238, 11178,
    11038, 10938, 10838, 10478
]

assert_same_elements(list_from_web, correctly_sorted_list)
© www.soinside.com 2019 - 2024. All rights reserved.