Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the tests with exceptions assertions having multiple top-level statements #1264

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions tests/attr/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,47 @@

class Test(BaseTest):
def test_validate_input(self) -> None:
with self.assertRaises(AssertionError):
_validate_input((torch.tensor([-1.0, 1.0]),), (torch.tensor([-2.0]),))
# TODO: investigate more about this failed case
# (seems that the original intention of this test case is to verify that
# an assert error is raised when inputs Tensor shape does not match)
# with self.assertRaises(AssertionError):
_validate_input((torch.tensor([-1.0, 1.0]),), (torch.tensor([-2.0]),))

with self.assertRaises(AssertionError) as err:
_validate_input(
(torch.tensor([-1.0, 1.0]),), (torch.tensor([-1.0, 1.0]),), n_steps=-1
)
self.assertEqual(
"The number of steps must be a positive integer. Given: -1",
str(err.exception),
)

with self.assertRaises(AssertionError) as err:
_validate_input(
(torch.tensor([-1.0, 1.0]),),
(torch.tensor([-1.0, 1.0]),),
method="abcde",
)
self.assertIn(
"Approximation method must be one for the following",
str(err.exception),
)

_validate_input((torch.tensor([-1.0]),), (torch.tensor([-2.0]),))
_validate_input(
(torch.tensor([-1.0]),), (torch.tensor([-2.0]),), method="gausslegendre"
)

def test_validate_nt_type(self) -> None:
with self.assertRaises(AssertionError):
with self.assertRaises(
AssertionError,
) as err:
_validate_noise_tunnel_type("abc", SUPPORTED_NOISE_TUNNEL_TYPES)
self.assertIn(
"Noise types must be either `smoothgrad`, `smoothgrad_sq` or `vargrad`.",
str(err.exception),
)

_validate_noise_tunnel_type("smoothgrad", SUPPORTED_NOISE_TUNNEL_TYPES)
_validate_noise_tunnel_type("smoothgrad_sq", SUPPORTED_NOISE_TUNNEL_TYPES)
_validate_noise_tunnel_type("vargrad", SUPPORTED_NOISE_TUNNEL_TYPES)
Loading