diff options
author | 2020-02-29 17:06:51 +0100 | |
---|---|---|
committer | 2020-02-29 17:06:51 +0100 | |
commit | c2af442676011eb620593505789be4d34da76ea3 (patch) | |
tree | 05a89c295bdfc74ad7438d4e7a3a6ff2bff55fb1 /tests/base.py | |
parent | Merge branch 'master' into python38-migration (diff) |
Migrate snekbox tests to Python 3.8's unittest
I've migrated the `tests/test_snekbox.py` file to use the new Python 3.8-style unittests instead of our old style using our custom Async mocks.
In particular, I had to make a few changes:
- Mocking the async post() context manager correctly
Since `ClientSession.post` returns an async context manager when called, we need to make sure to assign the return value to the __aenter__ method of whatever `post()` returns, not of `post` itself (i.e.. when it's not called).
- Use the new AsyncMock assert methods `assert_awaited_once` and `assert_awaited_once_with`
Objects of the new `unittest.mock.AsyncMock` class have special methods to assert what they were called with that also assert that specific coroutine object was awaited. This means we test two things in one: Whether or not it was called with the right arguments and whether or not the returned coroutine object was then awaited.
- Patch `functools.partial` as `partial` objects are compared by identity
When you create two partial functions of the same function, you'll end up with two different `partial` objects. Since `partial` objects are compared by identity, you can't compare a `partial` created in a test method to that created in the callable you're trying to test. They will always compare as `False`. Since we're not interested in actually creating `partial` objects, I've just patched `functools.partial` in the namespace of the module we're testing to make sure we can compare them.
Diffstat (limited to 'tests/base.py')
0 files changed, 0 insertions, 0 deletions