Skip to main content

Pytest Approxable Mixin


Example

from pytest_approxable import Approxable
from dataclasses import dataclass
from pytest import approx

@dataclass
class MetalProperties(Approxable):
    name: str
    heat_capacity: float
    conductivity: float

def test_metal_equal():
    mp1 = MetalProperties(name='metal1', heat_capacity=0.5, conductivity=1.)
    mp2 = MetalProperties(name='metal2', heat_capacity=0.499999999, conductivity=0.9999999998)
    assert approx(mp1.approxable_dict, abs=0.1) == mp2.approxable_dict

Problem

When testing codes, we usually approximate floating numbers to avoid decimal points error. To do so, we use pytest.approx as follow

from pytest import approx
def test_approx():
    a = 1.0000000000000001
    b = 1.
    assert approx(a, abs=0.1) == b # this will pass
    assert a == b # this will fail

Moreover, we can also use this with the dictionary derived from a dataclass. For example,

from dataclasses import dataclass, asdict
from pytest import approx

@dataclass
class MetalProperties:
    heat_capacity: float
    conductivity: float

def test_metal_equal():
    mp1 = MetalProperties(heat_capacity=0.5, conductivity=1.)
    mp2 = MetalProperties(heat_capacity=0.499999999, conductivity=0.9999999998)
    assert approx(asdict(mp1), abs=0.1) == asdict(mp2)

Now the problem arises when we have non-number fields in the class

@dataclass
class MetalProperties:
    name: str
    heat_capacity: float
    conductivity: float

def test_metal_equal():
    mp1 = MetalProperties(name='metal1', heat_capacity=0.5, conductivity=1.)
    mp2 = MetalProperties(name='metal2', heat_capacity=0.499999999, conductivity=0.9999999998)
    assert approx(asdict(mp1), abs=0.1) == asdict(mp2)

Above code will fails since attribute name can't be approximated

##Progress

  • tests
  • supports nested object

Metadata

Release files for hackinteach-pytest-approxable 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hackinteach-pytest-approxable 0.0.3
File Size Uploaded
hackinteach-pytest-approxable-0.0.3.tar.gz 1.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hackinteach-pytest-approxable 0.0.3
File Interpreter ABI Platform
hackinteach_pytest_approxable-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / hackinteach-pytest-approxable-0.0.3.tar.gz

Download URL hackinteach-pytest-approxable-0.0.3.tar.gz
Size 1.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f5ff70b38727c712d76971b7292449450c6c710af2e9fb44a57ff5245f0ab5b5
BLAKE2b-256 checksum
How to use checksums
8034a8edf01a55cac03c9a2e6678274834ad3610d115347773301d55239cdc53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.3

Release files / hackinteach_pytest_approxable-0.0.3-py3-none-any.whl

Download URL hackinteach_pytest_approxable-0.0.3-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b64193c048c5221f9e285c5c0f2c334133c7586dc2e9a09f1ba085c4b7ebde2
BLAKE2b-256 checksum
How to use checksums
af85faca579be3945acef86d254f1b2feccb3f59c8b21a80ef1874d6e045cf54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page