Giter Site home page Giter Site logo

2bndy5 / circuitpython-mocks Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 52 KB

A library of mock data structures for soft testing CircuitPython-based projects

Home Page: https://circuitpython-mocks.rtfd.io/

License: MIT License

Python 100.00%
circuitpython circuitpython-library coverage-testing mock pytest test testing testing-library

circuitpython-mocks's Introduction

CI status Documentation Status Coverage Status PyPI - Version Pepy Total Downlods

Formatted by ruff PyPI - License PyPI - Python Version

Read The Docs

Documentation for this library is hosted at https://circuitpython-mocks.rtfd.io/

About this Library

This is a library of mock data structures to be used in soft-testing Circuitpython-based projects.

circuitpython-mocks's People

Contributors

2bndy5 avatar

Stargazers

Paul Cutler avatar

Watchers

 avatar

circuitpython-mocks's Issues

improve UART mock

Certain functions/attributes are not present in the mock busio.UART class:

  • UART.in_waiting should attempt to read the length of bytes in the next expected operation. Note, this shall not assert that the next expected operation is of type UARTRead.
  • UART.reset_input_buffer() should assert that a UARTFlush operation is expected. Note, the UARTFlush operation does not currently exist and should be introduced.
  • UART.timeout may be just a dummy attribute. Note, this could be used to check against a UART operation if the UART operations support a dummy wait or timeout attribute. Users should pass a None value to the UARTRead constructor.
  • UART.baudrate property should return the value given to the constructor. Note, this should be a read/write property.

move pytest fixture into submodule

It seems that pytest will use the monkey_patch_sys_paths fixture if anything is imported from circuitpython_mocks package (from the test module). It might be better to move this fixture into a submodule instead of residing in the package's root.

add `I2CScan` operation

Currently, there's no way to manipulate the I2C.scan() results without using pytest.MonkeyPatch fixture. It would be more convenient to provide a new operation in expectations that returns a mocked result for I2c.scan() calls.

implement mocks for `board.SPI` and `board.I2C` classes

I forgot to implement this before first release. It should be added.

FYI, board.SPI and board.I2C is not available for all CircuitPython boards, so I think its generally discouraged to use them despite their convenience...

Add more pins to the mock `board` module

This is a suggestion from discord, so I'm not sure how well it will pan out

The only thing i didnt like on a quick glance over the code is that you have a bazillion pin names listed on board.py, not really because the duplication but because you are probably missing other pin names used on some board

You can probably do something like

__pins: dict[str, Pin] = {}

def __getattr__(name: str) -> Pin:
    if name not in __pins:
        return __pins[name] = Pin()

    return __pins[name]

Generating pins dynamically at runtime will not work because there needs to be concrete constants declared in the board module.

Prefer using setup.py to generate (at pkg build time) the declared pins. This may involve using a submodule and just importing __all__ from the generated submodule.

reconsider how to assert that expectations are used

Currently, the expectations are checked that they are all used upon destruction of the mocked object. However, pytest will ignore any exception thrown by a destructor. This leaves a traceback in the pytest output about "Ignored exception" when some exceptions were not used.

Possible solutions

  1. let unused expectations go unchecked

    pros cons
    The done() method can be called from the test code This may cause a deviation from expected behavior
  2. assert that done() is called upon destruction

    pros cons
    This serves as reminder to update the test code This seems to have poor behavior with the monkey_patch_sys_paths fixture because it is set to autouse=True (see also #9 )

Additional context

Because pytest manages setup and tear-down as an internal implementation detail, this is destructor approach is rather undesirable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.