Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assert lists equality with pytest

I'm trying to make some unit tests with pytest.

I was thinking about doing things like that:

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

The first assertion in ok but with the second I have an value error.

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

I assume it is not the right way to assert the equality of two different lists with pytest.

How can I assert that the dataframe columns (a list) is equal to the expected one?

Thanks

like image 547
bAN Avatar asked Oct 24 '17 15:10

bAN


1 Answers

See this:

Note:

You can simply use the assert statement for asserting test expectations. pytest’s Advanced assertion introspection will intelligently report intermediate values of the assert expression freeing you from the need to learn the many names of JUnit legacy methods.

And this:

Special comparisons are done for a number of cases:

  • comparing long strings: a context diff is shown
  • comparing long sequences: first failing indices
  • comparing dicts: different entries

And the reporting demo:

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 

See the assertion for lists equality with literal == over there? pytest has done the hard work for you.

like image 128
Rockallite Avatar answered Oct 03 '22 17:10

Rockallite