diff --git a/src/registrar/tests/common.py b/src/registrar/tests/common.py index ad464bb3e..6bcd3e15f 100644 --- a/src/registrar/tests/common.py +++ b/src/registrar/tests/common.py @@ -58,7 +58,7 @@ def get_handlers(): @contextmanager -def less_console_noise(): +def less_console_noise(output_stream=None): """ Context manager to use in tests to silence console logging. @@ -66,14 +66,19 @@ def less_console_noise(): (such as errors) which are normal and expected. It can easily be removed to debug a failing test. + + Arguments: + `output_stream`: a stream to redirect every handler to. If it's + not provided, use /dev/null. """ restore = {} handlers = get_handlers() - devnull = open(os.devnull, "w") + if output_stream is None: + output_stream = open(os.devnull, "w") # redirect all the streams for handler in handlers.values(): - prior = handler.setStream(devnull) + prior = handler.setStream(output_stream) restore[handler.name] = prior try: # run the test @@ -82,8 +87,9 @@ def less_console_noise(): # restore the streams for handler in handlers.values(): handler.setStream(restore[handler.name]) - # close the file we opened - devnull.close() + if output_stream is None: + # we opened output_stream so we have to close it + output_stream.close() class MockUserLogin: diff --git a/src/registrar/tests/test_transition_domain_migrations.py b/src/registrar/tests/test_transition_domain_migrations.py index 7fd243073..c33f9ff57 100644 --- a/src/registrar/tests/test_transition_domain_migrations.py +++ b/src/registrar/tests/test_transition_domain_migrations.py @@ -272,11 +272,12 @@ class TestLogins(TestCase): # this is one of the email addresses in data/test_contacts.txt output_stream = StringIO() - call_command("send_domain_invitations", - stdout=output_stream) + # also have to re-point the logging handlers to output_stream + with less_console_noise(output_stream): + call_command("send_domain_invitations", "testuser@gmail.com", stdout=output_stream) # Check that we had the right numbers in our output output = output_stream.getvalue() - print("Output:", output) + # should only be one domain we send email for self.assertIn("Found 1 transition domains", output) self.assertTrue("would send email to testuser@gmail.com", output)