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
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.
all
True
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
See this :
Note:assert . pytests assert JUnit legacy .
Note:
assert . pytests assert JUnit legacy .
assert
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 .
==
unittest.TestCase, , : unittest.TestCase.assertListEqual, , unittest.TestCase.assertCountEqual, .
unittest.TestCase
unittest.TestCase.assertListEqual
unittest.TestCase.assertCountEqual
https://docs.python.org/3.5/library/unittest.html#unittest.TestCase.assertCountEqual
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.
any
Use function numpy array_equal:
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)
Source: https://habr.com/ru/post/1688096/More articles:Java Hashcode and Equals for Java 8 functional interface objects - javaМожет кто-нибудь объяснить, что происходит с заключительной частью этого примера RSA? - algorithmggplot2 width of boxplot - rIOS 11 crash with VectorKit and Texture2D - mapkitGoogle Pubsub: UNAVAILABLE: the service could not fulfill your request - google-app-engineD3 Pie in Angular 2? - angularRecursive type bound - semantic - javaИзменить выравнивание текста после первой строки - htmlScripts not loading from .angular-cli.json - javascriptVespa: Is it possible to aggregate on nested fields? - vespaAll Articles