Refactor test_commandhandler.py #1408
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
multiple mock objects were needed in the same test function
global constants)
conftest.pyto be used by othermodules
TestCommandHandlerandTestPrefixHandler, extracting common methods, patterns andsignatures
leading
_test(e.g.
is_match) and methods (e.g.make_default_handler)Rationale
The main goal was to improve maintainability. Here are some of the reasons for each of the changes.
Replacing fixtures for factory functions
Some test functions requested a fixture and used the same object multiple times, "patching" the object with the necessary changes to make each assertion, which was error-prone and cluttered the code. For example, this is how
test_basicinTestCommandHandlerwas before:The single
messagefixture is "reused" by patching themessage.textfield every time. That leads to a broken test, though, since themessage.entitiesfield isn't being changed appropriately: for example, if the third text was'atest /test at start'instead ofnot /test at start, the corresponding assertion would fail (since the entity inmessage.entitiesstill pointed atmessage.text[0]), and that's not the intended behaviour.Now this test looks like this:
Where
commandis a fixture for a command, like'/test', andmake_command_updateis a factory that handles making an accuratetelegram.Messagewith the appropiate entity.Base class for both test classes
The two test classes,
TestCommandHandlerandTestPrefixHandler, were very very similar, so a base class for both with common fixtures, callbacks and other utility methods helped a lot in cleaning the codeFixtures in
conftest.pySome fixtures were moved to
conftest.pyso that they can be used by other test modules. Grouping test modules into subdirectories with their ownconftest.pywith shared fixtures and utilities would be ideal, but I only worked ontest_commandhandler.py.Other details
Tests for
pass_*I had a hard time understanding why
pass_user_data,pass_chat_data,pass_job_queue, andpass_update_queuewere tested in pairs:In any case, due to their similarity, I condensed together in a single parametrised test method:
I kept the "testing in pairs" by parametrising the
pass_combinationfixture appropriately: