mrsonandrade / pyswarming

A research toolkit for Swarm Robotics.
https://pyswarming.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
21 stars 3 forks source link

Increase Code Coverage #6

Closed JHartzer closed 1 year ago

JHartzer commented 1 year ago

I recommend making use of pytest-cov or any other coverage tool to calculate and track overall code coverage. I've included an initial high-level pass at the current state of testing below:

Module statements missing excluded coverage
pyswarming/init.py 11 0 0 100%
pyswarming/auto_differentiation.py 160 91 0 43%
pyswarming/behaviors.py 160 19 0 88%
pyswarming/swarm.py 74 64 0 14%
tests/init.py 0 0 0 100%
tests/test_behaviors.py 312 0 0 100%
Total 717 174 0 76%

It is apparent that most of the main behavior functions are being tested, but the main swarm class is not. I believe adding tests for the swarm class initialization and simulation would improve this work. Simply adding the two examples in a test function would likely be sufficient.

mrsonandrade commented 1 year ago

@JHartzer, thank you for the recommendation! I did not know pytest-cov. I have worked on increasing the code coverage of the behaviors class and I was able to increase it from 88% to 98%. However, I could not handle that much the swarm class due to the plt.show() inside simulate(). So, it increased from 14% to 39% only. I would appreciate it if you have any suggestions on how to overcome that.

JHartzer commented 1 year ago

Excellent! Typically you can get around the plotting test coverage limitation by separating the simulation from the plotting routines. For example, if simulate() produced an array of state data, and a separate animate() function ingested those states and produced the animation. That way, you could test the simulation portion of the code and ignore the plotting.

mrsonandrade commented 1 year ago

Great! Thanks for the suggestion! Instead of separating and adding one more step to the new users, I have created a mode argument for simulate(), so, it is possible to just get the states, get the animation object, or produce the animation. Now the code coverage increased from 39% to 82% 😃