Skip to content

pytest.approx doesn't support comparison between arrays and scalars #3312

@tadeu

Description

@tadeu

Sometimes we want to assert that all elements from an array are approximately equal to a single value, so this code comes naturally to mind:

assert some_array == approx(1.0)

the problem is that pytest.approx doesn't support this (the workaround is to create a second array with the same shape and filled with the scalar).

Here is a test case:

from pytest import approx
import numpy as np

def test_approx_array_scalar():
    assert np.array([1.0, 1.0]) == approx(1.0)

def test_approx_scalar_array():
    assert 1.0 == approx(np.array([1.0, 1.0]))

here are the outputs:

______________________________________________ test_approx_array_scalar _______________________________________________
                                                                                                                       
    def test_approx_array_scalar():                                                                                    
>       assert np.array([1.0, 1.0]) == approx(1.0)                                                                     
E       ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()        
                                                                                                                       
test_approx.py:5: ValueError                                                                                           
______________________________________________ test_approx_scalar_array _______________________________________________
                                                                                                                       
    def test_approx_scalar_array():                                                                                    
>       assert 1.0 == approx(np.array([1.0, 1.0]))                                                                     
E       assert 1.0 == approx([1.0 ± 1.0e-06, 1.0 ± 1.0e-06])                                                           
E        +  where approx([1.0 ± 1.0e-06, 1.0 ± 1.0e-06]) = approx(array([ 1.,  1.]))                                   
E        +    where array([ 1.,  1.]) = <built-in function array>([1.0, 1.0])                                          
E        +      where <built-in function array> = np.array                                                             
                                                                                                                       
test_approx.py:8: AssertionError                                                                                       

This is pip list, including pytest version:

certifi (2016.2.28)
colorama (0.3.9)   
numpy (1.13.1)     
pip (9.0.1)        
py (1.4.34)        
pytest (3.2.1)     
setuptools (36.4.0)
wheel (0.29.0)     
wincertstore (0.2) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: help wanteddevelopers would like help from experts on this topictype: enhancementnew feature or API change, should be merged into features branchtype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions