Fix slow tests in python3.
Move def out of the loop, remove bool.
Merge pull request #2644 from jrioux/tests

Fix slow tests in python3.