close

How can I assert lists equality with pytest

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I assert lists equality with pytest in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How can I assert lists equality with pytest?

  1. How can I assert lists equality with pytest?

    You could do a list comprehension to check equality of all values. If you call all on the list comprehensions result, it will return True if all parameters are equal.

  2. assert lists equality with pytest

    You could do a list comprehension to check equality of all values. If you call all on the list comprehensions result, it will return True if all parameters are equal.

Method 1

You could do a list comprehension to check equality of all values. If you call all on the list comprehensions result, it will return True if all parameters are equal.

actual = ['bl', 'direction', 'day']
expected = ['bl', 'direction', 'day']

assert len(actual) == len(expected)
assert all([a == b for a, b in zip(actual, expected)])

print(all([a == b for a, b in zip(actual, expected)]))

>>> True

Method 2

In Python 3.9, this should now work:

def test_arrays_equal():
    a = [1, 2, 3]
    b = [1, 2, 4]
    assert a == b

Or, you can parse the lists to numpy arrays and use function array_equal:

import numpy as np

def test_arrays_equal():
    a = [1, 2, 3]
    b = [1, 2, 4]
    ar = np.array(a)
    br = np.array(b)
    assert np.array_equal(ar, br)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read