Open ddemidov opened 8 years ago
@rhempel, fake device driver would make our life as bindings developers extremely easy, but I think the effort required in order to develop such fake drivers (and keep them in sync with the original ones) would be too high. What I thought of is something like this: https://github.com/ddemidov/ev3dev-lang-python-1/commit/0d4db5603e6256c5d724399b4bcccdd474c2dd4e.
Here, I created fake sys/class
folder (currently contains single MediumMotor
instance), and a unit test. The unit test replaces the Device.DEVICE_ROOT_PATH
parameter with the fake path and checks that MediumMotor
is able to connect and that the motor properties coincide with what actually is stored in the fake device attributes.
Immediate outcome of this is that the unittest reported to me that the FileCache
class, contrary to what I thought, leaks file handles:
$ ./tests/api_tests.py
/usr/lib/python3.5/unittest/case.py:597: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/demidov/work/ev3/ev3dev-lang/python/tests/fake_sys_class/tacho-motor/motor0/count_per_rot' mode='r' encoding='UTF-8'>
testMethod()
/usr/lib/python3.5/unittest/case.py:597: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/demidov/work/ev3/ev3dev-lang/python/tests/fake_sys_class/tacho-motor/motor0/encoder_polarity' mode='r' encoding='UTF-8'>
testMethod()
...
which resulted in https://github.com/ddemidov/ev3dev-lang-python-1/commit/54a8c32fc48381b960c9fd08619b2cef437b1047. Now the complete output of api_test.py
looks like this:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
The advantage of such file-based tests is that we can easily run the tests on Travis-CI, after each commit pushed to github, fully automatically. In fact, I went ahead and implemented Travis-CI integration in the tests branch of my fork, so now I have a shiny badge at the top of the README, which says that all tests are passing. If you click on the badge, you see the log of the last test (triggered by the latest commit).
That's a "good enough" mock and will do the job admirably!
See the head of the discussion here: https://github.com/ddemidov/ev3dev-lang-python-1/commit/92828ecf6061b8e5f0a65664eb86082874b5ee89#commitcomment-14064704.
To keep the issue self contained:
@ddemidov:
@dlech:
@rhempel: