]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Refactor test cases to improve unit test quality (#3298)
authorHan Wang <freddie.wanah@gmail.com>
Sat, 8 Jun 2024 18:09:27 +0000 (04:09 +1000)
committerGitHub <noreply@github.com>
Sat, 8 Jun 2024 18:09:27 +0000 (14:09 -0400)
* refactor the code to avoid suboptimal asserts.

* Handle ParseError with assertRaises

Signed-off-by: Han Wang <freddie.wanah@gmail.com>
15 files changed:
tornado/test/auth_test.py
tornado/test/gen_test.py
tornado/test/httpclient_test.py
tornado/test/httpserver_test.py
tornado/test/httputil_test.py
tornado/test/ioloop_test.py
tornado/test/locks_test.py
tornado/test/netutil_test.py
tornado/test/process_test.py
tornado/test/simple_httpclient_test.py
tornado/test/tcpclient_test.py
tornado/test/template_test.py
tornado/test/util_test.py
tornado/test/web_test.py
tornado/test/websocket_test.py

index 5eddb9803cd78f9f08689206cbf2c4b36b9d6100..834f04ea309bdda995c22e7aefb557fee9861c22 100644 (file)
@@ -341,7 +341,7 @@ class AuthTest(AsyncHTTPTestCase):
     def test_openid_redirect(self):
         response = self.fetch("/openid/client/login", follow_redirects=False)
         self.assertEqual(response.code, 302)
-        self.assertTrue("/openid/server/authenticate?" in response.headers["Location"])
+        self.assertIn("/openid/server/authenticate?", response.headers["Location"])
 
     def test_openid_get_user(self):
         response = self.fetch(
@@ -363,9 +363,9 @@ class AuthTest(AsyncHTTPTestCase):
             )
         )
         # the cookie is base64('zxcv')|base64('1234')
-        self.assertTrue(
-            '_oauth_request_token="enhjdg==|MTIzNA=="'
-            in response.headers["Set-Cookie"],
+        self.assertIn(
+            '_oauth_request_token="enhjdg==|MTIzNA=="',
+            response.headers["Set-Cookie"],
             response.headers["Set-Cookie"],
         )
 
@@ -385,8 +385,8 @@ class AuthTest(AsyncHTTPTestCase):
         parsed = json_decode(response.body)
         self.assertEqual(parsed["oauth_consumer_key"], "asdf")
         self.assertEqual(parsed["oauth_token"], "uiop")
-        self.assertTrue("oauth_nonce" in parsed)
-        self.assertTrue("oauth_signature" in parsed)
+        self.assertIn("oauth_nonce", parsed)
+        self.assertIn("oauth_signature", parsed)
 
     def test_oauth10a_redirect(self):
         response = self.fetch("/oauth10a/client/login", follow_redirects=False)
@@ -427,8 +427,8 @@ class AuthTest(AsyncHTTPTestCase):
         parsed = json_decode(response.body)
         self.assertEqual(parsed["oauth_consumer_key"], "asdf")
         self.assertEqual(parsed["oauth_token"], "uiop")
-        self.assertTrue("oauth_nonce" in parsed)
-        self.assertTrue("oauth_signature" in parsed)
+        self.assertIn("oauth_nonce", parsed)
+        self.assertIn("oauth_signature", parsed)
 
     def test_oauth10a_get_user_coroutine_exception(self):
         response = self.fetch(
@@ -440,7 +440,7 @@ class AuthTest(AsyncHTTPTestCase):
     def test_oauth2_redirect(self):
         response = self.fetch("/oauth2/client/login", follow_redirects=False)
         self.assertEqual(response.code, 302)
-        self.assertTrue("/oauth2/server/authorize?" in response.headers["Location"])
+        self.assertIn("/oauth2/server/authorize?", response.headers["Location"])
 
     def test_facebook_login(self):
         response = self.fetch("/facebook/client/login", follow_redirects=False)
@@ -464,9 +464,9 @@ class AuthTest(AsyncHTTPTestCase):
             )
         )
         # the cookie is base64('zxcv')|base64('1234')
-        self.assertTrue(
-            '_oauth_request_token="enhjdg==|MTIzNA=="'
-            in response.headers["Set-Cookie"],
+        self.assertIn(
+            '_oauth_request_token="enhjdg==|MTIzNA=="',
+            response.headers["Set-Cookie"],
             response.headers["Set-Cookie"],
         )
 
@@ -486,9 +486,9 @@ class AuthTest(AsyncHTTPTestCase):
             response.headers["Location"],
         )
         # the cookie is base64('zxcv')|base64('1234')
-        self.assertTrue(
-            '_oauth_request_token="enhjdg==|MTIzNA=="'
-            in response.headers["Set-Cookie"],
+        self.assertIn(
+            '_oauth_request_token="enhjdg==|MTIzNA=="',
+            response.headers["Set-Cookie"],
             response.headers["Set-Cookie"],
         )
 
index c17bf65fd00f0be8d8a4ba6fc2bb427c1e7157be..b495636561a9651e146ab2dbd0c88f3d1ad25bac 100644 (file)
@@ -437,7 +437,7 @@ class GenCoroutineTest(AsyncTestCase):
             return
 
         result = yield f()
-        self.assertEqual(result, None)
+        self.assertIsNone(result)
         self.finished = True
 
     @gen_test
@@ -449,7 +449,7 @@ class GenCoroutineTest(AsyncTestCase):
             return
 
         result = yield f()
-        self.assertEqual(result, None)
+        self.assertIsNone(result)
         self.finished = True
 
     @gen_test
@@ -602,7 +602,7 @@ class GenCoroutineTest(AsyncTestCase):
 
         self.io_loop.run_sync(inner2, timeout=3)
 
-        self.assertIs(self.local_ref(), None)
+        self.assertIsNone(self.local_ref())
         self.finished = True
 
     def test_asyncio_future_debug_info(self):
@@ -803,8 +803,8 @@ class WaitIteratorTest(AsyncTestCase):
         with self.assertRaises(ValueError):
             g = gen.WaitIterator(Future(), bar=Future())
 
-        self.assertEqual(g.current_index, None, "bad nil current index")
-        self.assertEqual(g.current_future, None, "bad nil current future")
+        self.assertIsNone(g.current_index, "bad nil current index")
+        self.assertIsNone(g.current_future, "bad nil current future")
 
     @gen_test
     def test_already_done(self):
@@ -835,8 +835,8 @@ class WaitIteratorTest(AsyncTestCase):
                 self.assertEqual(r, 84)
             i += 1
 
-        self.assertEqual(g.current_index, None, "bad nil current index")
-        self.assertEqual(g.current_future, None, "bad nil current future")
+        self.assertIsNone(g.current_index, "bad nil current index")
+        self.assertIsNone(g.current_future, "bad nil current future")
 
         dg = gen.WaitIterator(f1=f1, f2=f2)
 
@@ -856,9 +856,8 @@ class WaitIteratorTest(AsyncTestCase):
                 self.fail("got bad WaitIterator index {}".format(dg.current_index))
 
             i += 1
-
-        self.assertEqual(dg.current_index, None, "bad nil current index")
-        self.assertEqual(dg.current_future, None, "bad nil current future")
+        self.assertIsNone(g.current_index, "bad nil current index")
+        self.assertIsNone(g.current_future, "bad nil current future")
 
     def finish_coroutines(self, iteration, futures):
         if iteration == 3:
@@ -998,12 +997,12 @@ class RunnerGCTest(AsyncTestCase):
         loop.close()
         gc.collect()
         # Future was collected
-        self.assertIs(wfut[0](), None)
+        self.assertIsNone(wfut[0]())
         # At least one wakeup
         self.assertGreaterEqual(len(result), 2)
         if not self.is_pypy3():
             # coroutine finalizer was called (not on PyPy3 apparently)
-            self.assertIs(result[-1], None)
+            self.assertIsNone(result[-1])
 
     def test_gc_infinite_async_await(self):
         # Same as test_gc_infinite_coro, but with a `async def` function
@@ -1034,12 +1033,12 @@ class RunnerGCTest(AsyncTestCase):
             loop.close()
             gc.collect()
         # Future was collected
-        self.assertIs(wfut[0](), None)
+        self.assertIsNone(wfut[0]())
         # At least one wakeup and one finally
         self.assertGreaterEqual(len(result), 2)
         if not self.is_pypy3():
             # coroutine finalizer was called (not on PyPy3 apparently)
-            self.assertIs(result[-1], None)
+            self.assertIsNone(result[-1])
 
     def test_multi_moment(self):
         # Test gen.multi with moment
index 17291f8f2e8151a5a81e0b19886a791bb4092259..793dce13c5a5bdffac4a39ef436d092263aaacaa 100644 (file)
@@ -690,12 +690,12 @@ X-XSS-Protection: 1;
         start_time = time.time()
         response = self.fetch("/hello")
         response.rethrow()
-        assert response.request_time is not None
+        self.assertIsNotNone(response.request_time)
         self.assertGreaterEqual(response.request_time, 0)
         self.assertLess(response.request_time, 1.0)
         # A very crude check to make sure that start_time is based on
         # wall time and not the monotonic clock.
-        assert response.start_time is not None
+        self.assertIsNotNone(response.start_time)
         self.assertLess(abs(response.start_time - start_time), 1.0)
 
         for k, v in response.time_info.items():
@@ -763,7 +763,7 @@ class RequestProxyTest(unittest.TestCase):
 
     def test_neither_set(self):
         proxy = _RequestProxy(HTTPRequest("http://example.com/"), dict())
-        self.assertIs(proxy.auth_username, None)
+        self.assertIsNone(proxy.auth_username)
 
     def test_bad_attribute(self):
         proxy = _RequestProxy(HTTPRequest("http://example.com/"), dict())
@@ -772,7 +772,7 @@ class RequestProxyTest(unittest.TestCase):
 
     def test_defaults_none(self):
         proxy = _RequestProxy(HTTPRequest("http://example.com/"), None)
-        self.assertIs(proxy.auth_username, None)
+        self.assertIsNone(proxy.auth_username)
 
 
 class HTTPResponseTestCase(unittest.TestCase):
index 0b29a39ccf03af7dfca7d2b6ebb06f4a7099fed1..762c23f6e03fdbba5e21477d84b7df85298b692d 100644 (file)
@@ -989,8 +989,8 @@ class KeepAliveTest(AsyncHTTPTestCase):
         self.stream.write(b"GET / HTTP/1.0\r\n\r\n")
         yield self.read_response()
         data = yield self.stream.read_until_close()
-        self.assertTrue(not data)
-        self.assertTrue("Connection" not in self.headers)
+        self.assertFalse(data)
+        self.assertNotIn("Connection", self.headers)
         self.close()
 
     @gen_test
index 6d618839e07f820d41b07aafdf330707cd0c35f2..75c92fffd9ee5b5fac9178fc312bcd8e479e0201 100644 (file)
@@ -474,7 +474,7 @@ class HTTPServerRequestTest(unittest.TestCase):
         request = HTTPServerRequest(
             uri="/", headers=HTTPHeaders({"Canary": ["Coal Mine"]})
         )
-        self.assertTrue("Canary" not in repr(request))
+        self.assertNotIn("Canary", repr(request))
 
 
 class ParseRequestStartLineTest(unittest.TestCase):
index d07438aa6321ab9223f65667b5df522d1cfbfaa7..07a7cf0c3068da18c30ab20cc53351c357fecbb1 100644 (file)
@@ -243,17 +243,17 @@ class TestIOLoop(AsyncTestCase):
         # All the timeout methods return non-None handles that can be
         # passed to remove_timeout.
         handle = self.io_loop.add_timeout(self.io_loop.time(), lambda: None)
-        self.assertFalse(handle is None)
+        self.assertIsNotNone(handle)
         self.io_loop.remove_timeout(handle)
 
     def test_call_at_return(self):
         handle = self.io_loop.call_at(self.io_loop.time(), lambda: None)
-        self.assertFalse(handle is None)
+        self.assertIsNotNone(handle)
         self.io_loop.remove_timeout(handle)
 
     def test_call_later_return(self):
         handle = self.io_loop.call_later(0, lambda: None)
-        self.assertFalse(handle is None)
+        self.assertIsNotNone(handle)
         self.io_loop.remove_timeout(handle)
 
     def test_close_file_object(self):
@@ -503,7 +503,7 @@ class TestIOLoopFutures(AsyncTestCase):
             )
             future = self.wait()
             self.assertTrue(future.done())
-            self.assertTrue(future.result() is None)
+            self.assertIsNone(future.result())
 
     @gen_test
     def test_run_in_executor_gen(self):
index 23e1c520490883e52d16fab84a161cea3e61b5ef..61e01c5790d938bbbfdab643e4990981733f6327 100644 (file)
@@ -346,7 +346,7 @@ class SemaphoreContextManagerTest(AsyncTestCase):
     def test_context_manager(self):
         sem = locks.Semaphore()
         with (yield sem.acquire()) as yielded:
-            self.assertTrue(yielded is None)
+            self.assertIsNone(yielded)
 
         # Semaphore was released and can be acquired again.
         self.assertTrue(asyncio.ensure_future(sem.acquire()).done())
@@ -358,7 +358,7 @@ class SemaphoreContextManagerTest(AsyncTestCase):
 
         async def f():
             async with sem as yielded:
-                self.assertTrue(yielded is None)
+                self.assertIsNone(yielded)
 
         yield f()
 
index b35b7947f1a5269f01948d966f274b39bffae819..b13c5cf0864dafc98542337698f29c00df59d1eb 100644 (file)
@@ -203,15 +203,15 @@ class IsValidIPTest(unittest.TestCase):
         self.assertTrue(is_valid_ip("4.4.4.4"))
         self.assertTrue(is_valid_ip("::1"))
         self.assertTrue(is_valid_ip("2620:0:1cfe:face:b00c::3"))
-        self.assertTrue(not is_valid_ip("www.google.com"))
-        self.assertTrue(not is_valid_ip("localhost"))
-        self.assertTrue(not is_valid_ip("4.4.4.4<"))
-        self.assertTrue(not is_valid_ip(" 127.0.0.1"))
-        self.assertTrue(not is_valid_ip(""))
-        self.assertTrue(not is_valid_ip(" "))
-        self.assertTrue(not is_valid_ip("\n"))
-        self.assertTrue(not is_valid_ip("\x00"))
-        self.assertTrue(not is_valid_ip("a" * 100))
+        self.assertFalse(is_valid_ip("www.google.com"))
+        self.assertFalse(is_valid_ip("localhost"))
+        self.assertFalse(is_valid_ip("4.4.4.4<"))
+        self.assertFalse(is_valid_ip(" 127.0.0.1"))
+        self.assertFalse(is_valid_ip(""))
+        self.assertFalse(is_valid_ip(" "))
+        self.assertFalse(is_valid_ip("\n"))
+        self.assertFalse(is_valid_ip("\x00"))
+        self.assertFalse(is_valid_ip("a" * 100))
 
 
 class TestPortAllocation(unittest.TestCase):
index 1f55f4d935cd200d4bd6c296428aa7ebb6912449..8b1f8635f43ccaa7da8d103c7ce2e8d4b0f93b60 100644 (file)
@@ -65,13 +65,13 @@ class ProcessTest(unittest.TestCase):
             signal.alarm(5)  # master process
             try:
                 id = fork_processes(3, max_restarts=3)
-                self.assertTrue(id is not None)
+                self.assertIsNotNone(id)
                 signal.alarm(5)  # child processes
             except SystemExit as e:
                 # if we exit cleanly from fork_processes, all the child processes
                 # finished with status 0
                 self.assertEqual(e.code, 0)
-                self.assertTrue(task_id() is None)
+                self.assertIsNone(task_id())
                 sock.close()
                 return
             try:
index 593f81fd341aaa955ea87ba69853b3fbd100bea3..50fdcb1536a7e6ea3292991405547ac2b271ed43 100644 (file)
@@ -179,10 +179,10 @@ class SimpleHTTPClientTestMixin(object):
 
     def test_singleton(self: typing.Any):
         # Class "constructor" reuses objects on the same IOLoop
-        self.assertTrue(SimpleAsyncHTTPClient() is SimpleAsyncHTTPClient())
+        self.assertIs(SimpleAsyncHTTPClient(), SimpleAsyncHTTPClient())
         # unless force_instance is used
-        self.assertTrue(
-            SimpleAsyncHTTPClient() is not SimpleAsyncHTTPClient(force_instance=True)
+        self.assertIsNot(
+            SimpleAsyncHTTPClient(), SimpleAsyncHTTPClient(force_instance=True)
         )
         # different IOLoops use different objects
         with closing(IOLoop()) as io_loop2:
@@ -193,7 +193,7 @@ class SimpleHTTPClientTestMixin(object):
 
             client1 = self.io_loop.run_sync(make_client)
             client2 = io_loop2.run_sync(make_client)
-            self.assertTrue(client1 is not client2)
+            self.assertIsNot(client1, client2)
 
     def test_connection_limit(self: typing.Any):
         with closing(self.create_client(max_clients=2)) as client:
index ecfc82d6fd5fd29ef644a116ab9160f8771ec98f..e1647925798e14a1385db34c48323ffb2ab41508 100644 (file)
@@ -362,7 +362,7 @@ class ConnectorTest(AsyncTestCase):
         self.resolve_connect(AF1, "a", True)
         conn.on_connect_timeout()
         self.assert_pending()
-        self.assertEqual(self.streams["a"].closed, False)
+        self.assertFalse(self.streams["a"].closed)
         # success stream will be pop
         self.assertEqual(len(conn.streams), 0)
         # streams in connector should be closed after connect timeout
index 801de50bc1183c13758bc6eeace997785efeaa39..477b762dc98b76b528611ac4383a70cec74df096 100644 (file)
@@ -164,22 +164,16 @@ try{% set y = 1/x %}
         self.assertEqual(result, b"013456")
 
     def test_break_outside_loop(self):
-        try:
+        with self.assertRaises(ParseError, msg="Did not get expected exception"):
             Template(utf8("{% break %}"))
-            raise Exception("Did not get expected exception")
-        except ParseError:
-            pass
 
     def test_break_in_apply(self):
         # This test verifies current behavior, although of course it would
         # be nice if apply didn't cause seemingly unrelated breakage
-        try:
+        with self.assertRaises(ParseError, msg="Did not get expected exception"):
             Template(
                 utf8("{% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}")
             )
-            raise Exception("Did not get expected exception")
-        except ParseError:
-            pass
 
     @unittest.skip("no testable future imports")
     def test_no_inherit_future(self):
@@ -267,7 +261,7 @@ three{%end%}
             self.fail("did not get expected exception")
         except ZeroDivisionError:
             exc_stack = traceback.format_exc()
-        self.assertTrue("# base.html:1" in exc_stack)
+        self.assertIn("# base.html:1", exc_stack)
 
     def test_error_line_number_extends_sub_error(self):
         loader = DictLoader(
@@ -285,7 +279,7 @@ three{%end%}
             loader.load("sub.html").generate()
             self.fail("did not get expected exception")
         except ZeroDivisionError:
-            self.assertTrue("# sub.html:4 (via base.html:1)" in traceback.format_exc())
+            self.assertIn("# sub.html:4 (via base.html:1)", traceback.format_exc())
 
     def test_multi_includes(self):
         loader = DictLoader(
@@ -299,9 +293,7 @@ three{%end%}
             loader.load("a.html").generate()
             self.fail("did not get expected exception")
         except ZeroDivisionError:
-            self.assertTrue(
-                "# c.html:1 (via b.html:1, a.html:1)" in traceback.format_exc()
-            )
+            self.assertIn("# c.html:1 (via b.html:1, a.html:1)", traceback.format_exc())
 
 
 class ParseErrorDetailTest(unittest.TestCase):
index 7a6e817fd2b892761d8c5b68a2e8499e28d659f0..e77ad9a9ad329099375fab42faf66957804bff8c 100644 (file)
@@ -113,7 +113,7 @@ class ConfigurableTest(unittest.TestCase):
         # let us access attributes of the base class.
         obj = cast(TestConfig1, TestConfigurable())
         self.assertIsInstance(obj, TestConfig1)
-        self.assertIs(obj.a, None)
+        self.assertIsNone(obj.a)
 
         obj = cast(TestConfig1, TestConfigurable(a=1))
         self.assertIsInstance(obj, TestConfig1)
@@ -125,7 +125,7 @@ class ConfigurableTest(unittest.TestCase):
         TestConfigurable.configure(TestConfig2)
         obj = cast(TestConfig2, TestConfigurable())
         self.assertIsInstance(obj, TestConfig2)
-        self.assertIs(obj.b, None)
+        self.assertIsNone(obj.b)
 
         obj = cast(TestConfig2, TestConfigurable(b=2))
         self.assertIsInstance(obj, TestConfig2)
@@ -137,7 +137,7 @@ class ConfigurableTest(unittest.TestCase):
         TestConfigurable.configure("tornado.test.util_test.TestConfig2")
         obj = cast(TestConfig2, TestConfigurable())
         self.assertIsInstance(obj, TestConfig2)
-        self.assertIs(obj.b, None)
+        self.assertIsNone(obj.b)
 
         obj = cast(TestConfig2, TestConfigurable(b=2))
         self.assertIsInstance(obj, TestConfig2)
@@ -159,7 +159,7 @@ class ConfigurableTest(unittest.TestCase):
         self.checkSubclasses()
         # args bound in configure don't apply when using the subclass directly
         obj = TestConfig1()
-        self.assertIs(obj.a, None)
+        self.assertIsNone(obj.a)
 
     def test_config_class_args(self):
         TestConfigurable.configure(TestConfig2, b=5)
@@ -175,7 +175,7 @@ class ConfigurableTest(unittest.TestCase):
         self.checkSubclasses()
         # args bound in configure don't apply when using the subclass directly
         obj = TestConfig2()
-        self.assertIs(obj.b, None)
+        self.assertIsNone(obj.b)
 
     def test_config_multi_level(self):
         TestConfigurable.configure(TestConfig3, a=1)
@@ -239,7 +239,7 @@ class ArgReplacerTest(unittest.TestCase):
     def test_omitted(self):
         args = (1, 2)
         kwargs = dict()  # type: Dict[str, Any]
-        self.assertIs(self.replacer.get_old_value(args, kwargs), None)
+        self.assertIsNone(self.replacer.get_old_value(args, kwargs))
         self.assertEqual(
             self.replacer.replace("new", args, kwargs),
             (None, (1, 2), dict(callback="new")),
index fec66f39ac8aeaa36436415d4e7b42a6774ea8ef..2fa146d130f15060964b1ba8351af85a985a433e 100644 (file)
@@ -131,7 +131,7 @@ class SecureCookieV1Test(unittest.TestCase):
         handler.set_signed_cookie("foo", binascii.a2b_hex(b"d76df8e7aefc"), version=1)
         cookie = handler._cookies["foo"]
         match = re.match(rb"12345678\|([0-9]+)\|([0-9a-f]+)", cookie)
-        assert match is not None
+        self.assertIsNotNone(match)
         timestamp = match.group(1)
         sig = match.group(2)
         self.assertEqual(
@@ -161,7 +161,7 @@ class SecureCookieV1Test(unittest.TestCase):
         )
         # it gets rejected
         with ExpectLog(gen_log, "Cookie timestamp in future"):
-            self.assertTrue(handler.get_signed_cookie("foo", min_version=1) is None)
+            self.assertIsNone(handler.get_signed_cookie("foo", min_version=1))
 
     def test_arbitrary_bytes(self):
         # Secure cookies accept arbitrary data (which is base64 encoded).
@@ -367,8 +367,9 @@ class CookieTest(WebTestCase):
         self.assertEqual(headers[0], 'equals="a=b"; Path=/')
         self.assertEqual(headers[1], 'quote="a\\"b"; Path=/')
         # python 2.7 octal-escapes the semicolon; older versions leave it alone
-        self.assertTrue(
-            headers[2] in ('semicolon="a;b"; Path=/', 'semicolon="a\\073b"; Path=/'),
+        self.assertIn(
+            headers[2],
+            ('semicolon="a;b"; Path=/', 'semicolon="a\\073b"; Path=/'),
             headers[2],
         )
 
@@ -400,15 +401,15 @@ class CookieTest(WebTestCase):
     def test_set_cookie_expires_days(self):
         response = self.fetch("/set_expires_days")
         header = response.headers.get("Set-Cookie")
-        assert header is not None
+        self.assertIsNotNone(header)
         match = re.match("foo=bar; expires=(?P<expires>.+); Path=/", header)
-        assert match is not None
+        self.assertIsNotNone(match)
 
         expires = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
             days=10
         )
         header_expires = email.utils.parsedate_to_datetime(match.groupdict()["expires"])
-        self.assertTrue(abs((expires - header_expires).total_seconds()) < 10)
+        self.assertLess(abs((expires - header_expires).total_seconds()), 10)
 
     def test_set_cookie_false_flags(self):
         response = self.fetch("/set_falsy_flags")
@@ -1044,15 +1045,15 @@ class ErrorResponseTest(WebTestCase):
         with ExpectLog(app_log, "Uncaught exception"):
             response = self.fetch("/default")
             self.assertEqual(response.code, 500)
-            self.assertTrue(b"500: Internal Server Error" in response.body)
+            self.assertIn(b"500: Internal Server Error", response.body)
 
             response = self.fetch("/default?status=503")
             self.assertEqual(response.code, 503)
-            self.assertTrue(b"503: Service Unavailable" in response.body)
+            self.assertIn(b"503: Service Unavailable", response.body)
 
             response = self.fetch("/default?status=435")
             self.assertEqual(response.code, 435)
-            self.assertTrue(b"435: Unknown" in response.body)
+            self.assertIn(b"435: Unknown", response.body)
 
     def test_write_error(self):
         with ExpectLog(app_log, "Uncaught exception"):
@@ -1122,10 +1123,10 @@ class StaticFileTest(WebTestCase):
 
     def test_static_files(self):
         response = self.fetch("/robots.txt")
-        self.assertTrue(b"Disallow: /" in response.body)
+        self.assertIn(b"Disallow: /", response.body)
 
         response = self.fetch("/static/robots.txt")
-        self.assertTrue(b"Disallow: /" in response.body)
+        self.assertIn(b"Disallow: /", response.body)
         self.assertEqual(response.headers.get("Content-Type"), "text/plain")
 
     def test_static_files_cacheable(self):
@@ -1133,7 +1134,7 @@ class StaticFileTest(WebTestCase):
         # test is pretty weak but it gives us coverage of the code path which
         # was important for detecting the deprecation of datetime.utcnow.
         response = self.fetch("/robots.txt?v=12345")
-        self.assertTrue(b"Disallow: /" in response.body)
+        self.assertIn(b"Disallow: /", response.body)
         self.assertIn("Cache-Control", response.headers)
         self.assertIn("Expires", response.headers)
 
@@ -1146,8 +1147,8 @@ class StaticFileTest(WebTestCase):
         )
         # make sure the uncompressed file still has the correct type
         response = self.fetch("/static/sample.xml")
-        self.assertTrue(
-            response.headers.get("Content-Type") in set(("text/xml", "application/xml"))
+        self.assertIn(
+            response.headers.get("Content-Type"), set(("text/xml", "application/xml"))
         )
 
     def test_static_url(self):
@@ -1206,7 +1207,7 @@ class StaticFileTest(WebTestCase):
             headers={"If-Modified-Since": response1.headers["Last-Modified"]},
         )
         self.assertEqual(response2.code, 304)
-        self.assertTrue("Content-Length" not in response2.headers)
+        self.assertNotIn("Content-Length", response2.headers)
 
     def test_static_304_if_none_match(self):
         response1 = self.get_and_head("/static/robots.txt")
@@ -1283,7 +1284,7 @@ class StaticFileTest(WebTestCase):
         with open(robots_file_path, encoding="utf-8") as f:
             self.assertEqual(response.body, utf8(f.read()))
         self.assertEqual(response.headers.get("Content-Length"), "26")
-        self.assertEqual(response.headers.get("Content-Range"), None)
+        self.assertIsNone(response.headers.get("Content-Range"))
 
     def test_static_with_range_full_past_end(self):
         response = self.get_and_head(
@@ -1294,7 +1295,7 @@ class StaticFileTest(WebTestCase):
         with open(robots_file_path, encoding="utf-8") as f:
             self.assertEqual(response.body, utf8(f.read()))
         self.assertEqual(response.headers.get("Content-Length"), "26")
-        self.assertEqual(response.headers.get("Content-Range"), None)
+        self.assertIsNone(response.headers.get("Content-Range"))
 
     def test_static_with_range_partial_past_end(self):
         response = self.get_and_head(
@@ -1332,7 +1333,7 @@ class StaticFileTest(WebTestCase):
         with open(robots_file_path, encoding="utf-8") as f:
             self.assertEqual(response.body, utf8(f.read()))
         self.assertEqual(response.headers.get("Content-Length"), "26")
-        self.assertEqual(response.headers.get("Content-Range"), None)
+        self.assertIsNone(response.headers.get("Content-Range"))
 
     def test_static_invalid_range(self):
         response = self.get_and_head("/static/robots.txt", headers={"Range": "asdf"})
@@ -1390,7 +1391,7 @@ class StaticFileTest(WebTestCase):
         )
         self.assertEqual(response.code, 304)
         self.assertEqual(response.body, b"")
-        self.assertTrue("Content-Length" not in response.headers)
+        self.assertNotIn("Content-Length", response.headers)
         self.assertEqual(
             utf8(response.headers["Etag"]), b'"' + self.robots_txt_hash + b'"'
         )
@@ -1670,7 +1671,7 @@ class ClearHeaderTest(SimpleHandlerTestCase):
 
     def test_clear_header(self):
         response = self.fetch("/")
-        self.assertTrue("h1" not in response.headers)
+        self.assertNotIn("h1", response.headers)
         self.assertEqual(response.headers["h2"], "bar")
 
 
@@ -1702,10 +1703,10 @@ class Header304Test(SimpleHandlerTestCase):
             "/", headers={"If-None-Match": response1.headers["Etag"]}
         )
         self.assertEqual(response2.code, 304)
-        self.assertTrue("Content-Length" not in response2.headers)
-        self.assertTrue("Content-Language" not in response2.headers)
+        self.assertNotIn("Content-Length", response2.headers)
+        self.assertNotIn("Content-Language", response2.headers)
         # Not an entity header, but should not be added to 304s by chunking
-        self.assertTrue("Transfer-Encoding" not in response2.headers)
+        self.assertNotIn("Transfer-Encoding", response2.headers)
 
 
 class StatusReasonTest(SimpleHandlerTestCase):
@@ -1744,9 +1745,9 @@ class DateHeaderTest(SimpleHandlerTestCase):
     def test_date_header(self):
         response = self.fetch("/")
         header_date = email.utils.parsedate_to_datetime(response.headers["Date"])
-        self.assertTrue(
-            header_date - datetime.datetime.now(datetime.timezone.utc)
-            < datetime.timedelta(seconds=2)
+        self.assertLess(
+            header_date - datetime.datetime.now(datetime.timezone.utc),
+            datetime.timedelta(seconds=2),
         )
 
 
@@ -2657,11 +2658,11 @@ class SignedValueTest(unittest.TestCase):
         decoded1 = decode_signed_value(
             SignedValueTest.SECRET, "key2", signed1, clock=self.present
         )
-        self.assertIs(decoded1, None)
+        self.assertIsNone(decoded1)
         decoded2 = decode_signed_value(
             SignedValueTest.SECRET, "key1", signed2, clock=self.present
         )
-        self.assertIs(decoded2, None)
+        self.assertIsNone(decoded2)
 
     def test_expired(self):
         signed = create_signed_value(
@@ -2674,7 +2675,7 @@ class SignedValueTest(unittest.TestCase):
         decoded_present = decode_signed_value(
             SignedValueTest.SECRET, "key1", signed, clock=self.present
         )
-        self.assertIs(decoded_present, None)
+        self.assertIsNone(decoded_present)
 
     def test_payload_tampering(self):
         # These cookies are variants of the one in test_known_values.
@@ -2763,7 +2764,7 @@ class SignedValueTest(unittest.TestCase):
         newkeys = SignedValueTest.SECRET_DICT.copy()
         newkeys.pop(0)
         decoded = decode_signed_value(newkeys, "key", signed, clock=self.present)
-        self.assertEqual(None, decoded)
+        self.assertIsNone(decoded)
 
     def test_key_version_retrieval(self):
         value = b"\xe9"
index 4d39f370467e8ced393a66e682d990314de74f02..8a3ac71252a41119156ab37a1a78141c28f39496 100644 (file)
@@ -378,7 +378,7 @@ class WebSocketTest(WebSocketBaseTestCase):
         ws.write_message("hello")
         with ExpectLog(app_log, "Uncaught exception"):
             response = yield ws.read_message()
-        self.assertIs(response, None)
+        self.assertIsNone(response)
 
     @gen_test
     def test_websocket_http_fail(self):