enthought / enthought/traits

Testing helper to push and pop exception handlers for both notification frameworks

Open
#1,323 3 comments 0 reactions 0 assignees View on GitHub
type: enhancement
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

In downstream projects using Traits, one often needs to add the following code in their test cases setUp and tearDown:
```
from traits.api import pop_exception_handler, push_exception_handler

class TestSomethingWithTraits(unittest.TestCase):

def setUp(self):
push_exception_handler(reraise_exceptions=True)

def tearDown(self):
pop_exception_handler()
```
Note that this `pop_exception_handler` and `push_exception_handler` are for `on_trait_change` only. `observe`, being a parallel framework, has its own pair of `pop_exception_handler` and `push_exception_handler` because (1) the functions allow the `handler` to be changed and the signature of `handler` is different in the two frameworks, and (2) the two frameworks are supposed to be independent.

So if a project is using both `on_trait_change` and `observe`, one has to do this to capture errors from both:
```
from traits.api import (
pop_exception_handler as pop_on_trait_change_handler,
push_exception_handler as push_on_trait_change_handler,
)
from traits.observation.api import (
pop_exception_handler as pop_observe_handler,
push_exception_handler as push_observe_handler,
)

class TestSomethingWithTraits(unittest.TestCase):

def setUp(self):
push_on_trait_change_handler(reraise_exceptions=True)
push_observe_handler(reraise_exceptions=True)

def tearDown(self):
pop_observe_handler()
pop_on_trait_change_handler()
```

Most of the use cases I have seen, `reraise_exceptions=True` is the only thing one would like to set in tests. It would be good to have a helper method that does this setup and teardown.

(I also wish there is an alternative to doing these things in `setUp` and `tearDown` for it is often cumbersome when dealing with multiple test base classes... that alternative might be a custom test runner, though.)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.