-
Notifications
You must be signed in to change notification settings - Fork 141
@Test.timeout decorator in python doesn't behave correctly anymore #698
Comments
I don't understand the "anymore" part. I haven't changed anything. @Bubbler-4 any idea what's going on? |
No idea either. I didn't know about throwing errors in threads when I wrote it. On the other hand, I found this SO answer to reraise child's exceptions in the parent. We can start working from there, though it sounds like a lot of work. Or, we can study and use pathos which is mentioned in a comment there. UPDATE: @test.timeout(1.0)
def test_one():
Test.expect_error('should not throw error', lambda: sol(some_args), ())
Test.assert_equals(sol(some_args), 1) So the def expect_no_error(s, f, err = BaseException):
try: f()
except err as e: test.fail(s) and the desired test case would be (also in this kumite fork) test.expect_no_error(
'Should not raise exceptions',
lambda: test.assert_equals(ulam_sequence(u0, u1, n), ulamRef(u0, u1, n))
) |
errr... "solitude" just posted that code as a fork. Is that actually one of you? x) ok, that effectively works, even if it's absolutely unusable for the average user, I'd say... :/ Would it be possible to make that internal to the timeout decorator in some way? I fear not, because this hack is still subject to the original problem. For instance: This one is catching correctly the exception raised in the code: def doTests(nTests, n):
a = range(1,100)
for i in range(nTests):
...
expect_no_error(
'Should not raise exceptions',
lambda: test.assert_equals(ulam_sequence(u0, u1, n), expected)
)
caller = lambda: doTests(20, 1500)
@Test.describe('Random tests')
def rnd():
@Test.it('Small sequences')
def smalls(): doTests(20, rand(10,31))
@Test.it('Large sequences')
def bigs():
@Test.timeout(11)
def withTimer(): caller() but this one doesn't anymore: def doTests(nTests, n):
a = range(1,100)
for i in range(nTests):
...
expect_no_error(
'Should not raise exceptions',
lambda: test.assert_equals(ulam_sequence(u0, u1, n), expected)
)
#caller = lambda: doTests(20, 1500)
@Test.describe('Random tests')
def rnd():
@Test.it('Small sequences')
def smalls(): doTests(20, rand(10,31))
@Test.it('Large sequences')
def bigs():
@Test.timeout(11)
def withTimer(): doTest(20,1500) #<<<<< |
You made a typo in the second code block ( And... yes, solitude is my Haskellish self, though it's invading many other areas these days. Sorry if you feel bad about it. |
x/ what the hell... Sorry... :/ edit: no, don't worry, I have no problem about your duplicity. ;) |
In order to avoid the hacks, I think this could work (given that we already have def timeout(limit):
...
def wrapper(func):
...
def child_process():
expect_no_error('Should not raise exceptions', func)
# create a child process with `child_process` as main
# run and terminate after `limit` seconds This is expected to work even without the external wrappers like Thinking about the use cases involving exceptions, you don't usually want to raise specific exceptions when you write a performance kata, and for @kazk Can I submit a PR on this repo, or are you planning to create a repo per language (for the ones with custom extensions, as in hspec-codewars)? |
@Bubbler-4 Created https://github.com/Codewars/python-test-framework with current cw-2.py so you can fork it and open PR with changes. |
PR submitted. Anyone interested can see the demo kumite. |
* Add `expect_no_error(message, function, exception=BaseException)` Tests if the function, when run, does not raise any exception that can be caught by `exception`. Generates a failed assertion if not met. * Improve `@timeout(sec)` The wrapped function is again wrapped inside `expect_no_error`, so that any exception raised inside the child process can be caught right there. If this happens, generates a failed assertion. * Fix formatting Fixes codewars/codewars-runner-cli#698
I'll let you guys know when this is deployed. |
Deployed. Thanks @Bubbler-4 for the fix. |
Describe the bug
Known fact: the decorator was tricky to use, but I had found a way and documented it in the wiki about the test framework.
To Reproduce
See this fork of anter69's solution to my "Ulam sequence" kata. I tried different things but only get the printing of the stack trace, the test suite is never crashing.
(edit: btw, there is something weird about the performances: I couldn't make my own solution to pass unless I lowered by 25% the load of the perf tests... x/. Tho that was valide before... That's unrelated to the current problem, tho. Just a note...)
Expected Behavior
Any exception thrown by the user or his code should interrupt the test with an actual failure.
Screenshot
The text was updated successfully, but these errors were encountered: