How can I claim equality is equal with pytest

I am trying to do some unit tests with pytest .

I thought about it:

actual = b_manager.get_b(complete_set)
assert actual is not None
assert actual.columns == ['bl', 'direction', 'day']

The first statement is ok, but with the second I have a value error.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I assume this is the wrong way to assert equality of two different lists with pytest.

How can I claim that the dataframe (list) columns are equal to the expected?

thank

+9
source share
5 answers

You can do a list check to check if all values ​​are equal. If you call the alllist comprehension result, it returns Trueif all parameters are equal.

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

assert all([a == b for a, b in zip(actual, ['bl', 'direction', 'day'])])

print(all([a == b for a, b in zip(actual, ['bl', 'direction', 'day'])]))

>>> True
+13
source

See this :

Note:

assert . pytests assert JUnit legacy .

this:

:

  • : diff
  • :
  • dicts:

:

failure_demo.py:59: AssertionError
_______ TestSpecialisedExplanations.test_eq_list ________

self = <failure_demo.TestSpecialisedExplanations object at 0xdeadbeef>

    def test_eq_list(self):
>       assert [0, 1, 2] == [0, 1, 3]
E       assert [0, 1, 2] == [0, 1, 3]
E         At index 2 diff: 2 != 3
E         Use -v to get the full diff

. == ? pytest .

+31

unittest.TestCase, , : unittest.TestCase.assertListEqual, , unittest.TestCase.assertCountEqual, .

https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertCountEqual

+5

convert numpy arrays to python lists and you will get a better answer than just using allor any. Thus, if the test fails, you will see how the expected and actual differences differ.

0
source

Use function numpy array_equal:

import numpy as np

test_arrays_equal():
    a = np.array([[1, 3], [2, 3], [1, 4], [2, 4], [1, 5], [2, 5]])
    b = np.array([[1, 3], [2, 3], [1, 4], [2, 4], [1, 5], [2, 5]])
    assert np.array_equal(a, b)
0
source

Source: https://habr.com/ru/post/1688096/


All Articles