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.py
to be used by othermodules
TestCommandHandler
andTestPrefixHandler
, 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_basic
inTestCommandHandler
was before:The single
message
fixture is "reused" by patching themessage.text
field every time. That leads to a broken test, though, since themessage.entities
field 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.entities
still pointed atmessage.text[0]
), and that's not the intended behaviour.Now this test looks like this:
Where
command
is a fixture for a command, like'/test'
, andmake_command_update
is a factory that handles making an accuratetelegram.Message
with the appropiate entity.Base class for both test classes
The two test classes,
TestCommandHandler
andTestPrefixHandler
, 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.py
Some fixtures were moved to
conftest.py
so that they can be used by other test modules. Grouping test modules into subdirectories with their ownconftest.py
with 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_queue
were 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_combination
fixture appropriately: