Exercise: Basic “Sensor”

Specification, And Setup

Implement a class ConstantSensor, such that the following works.

  • A program, read-sensor-const.py. Download that into the programs/ directory of your project structure.

    #!/usr/bin/env python
    
    from sensors.sensor_const import ConstantSensor
    
    sensor = ConstantSensor(36.7)
    temperature = sensor.get_temperature()
    print(temperature)
    
  • A unit test; you download that into the tests/ subdirectory of you project.

    from sensors.sensor_const import ConstantSensor
    import pytest
    
    def test_basic():
        sensor = ConstantSensor(36.7)
        temperature = sensor.get_temperature()
        assert temperature == pytest.approx(36.7)
    
  • To set paths, follow the instructions in Project Setup

Verification

Program

Run the program,

$ python programs/read-sensor-const.py
36.7

Unit Test

In you project’s root directory, unit tests like follows …

$ python -m pytest
============================= test session starts ==============================
platform linux -- Python 3.10.7, pytest-7.2.0, pluggy-1.0.0
rootdir: <your project root>
collected 1 item

tests/test_sensor_const.py .                                             [100%]

============================== 1 passed in 0.01s ===============================