Replace an assertRaises(StopIteration) by a next() with a default value. Following PEP 479, *any* StopIteration reaching the toplevel of a generator triggers a DeprecationWarning. And while the assertRaises looks innocuous, both _assertRaises and the underlying clear_upon_failure are implemented with functools.contextmanager, which wraps a generator. As a result, exceptions raised in the assertRaises "body" get reinjected inside the wrapped context manager[0] and if the exception is a StopIteration it triggers the warning. By not using assertRaises we side-step the issue. [0] https://github.com/python/cpython/blob/b6999e5690c7006b0ba4049cfd638513c982a90c/Lib/contextlib.py#L131