Skip to content

Commit

Permalink
Don't use docstring in tests. (#4163)
Browse files Browse the repository at this point in the history
1. Test code is not a part of public API
2. Test runners displays test docstring instead of test function if present,
   it complicates the code navigation, e.g. opening a failed test in editor etc.
  • Loading branch information
asvetlov authored Oct 9, 2019
1 parent e919a23 commit 1864880
Show file tree
Hide file tree
Showing 24 changed files with 91 additions and 134 deletions.
25 changes: 11 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
pytest_plugins = ['aiohttp.pytest_plugin', 'pytester']

IS_HPUX = sys.platform.startswith('hp-ux')
"""Specifies whether the current runtime is HP-UX."""
# Specifies whether the current runtime is HP-UX.
IS_LINUX = sys.platform.startswith('linux')
"""Specifies whether the current runtime is HP-UX."""
# Specifies whether the current runtime is HP-UX.
IS_UNIX = hasattr(socket, 'AF_UNIX')
"""Specifies whether the current runtime is *NIX."""
# Specifies whether the current runtime is *NIX.

needs_unix = pytest.mark.skipif(not IS_UNIX, reason='requires UNIX sockets')

Expand Down Expand Up @@ -100,23 +100,20 @@ def _proto_factory(conn_closing_result=None, **kwargs):

@pytest.fixture
def unix_sockname(tmp_path, tmp_path_factory):
"""Generate an fs path to the UNIX domain socket for testing.
# Generate an fs path to the UNIX domain socket for testing.

N.B. Different OS kernels have different fs path length limitations
for it. For Linux, it's 108, for HP-UX it's 92 (or higher) depending
on its version. For for most of the BSDs (Open, Free, macOS) it's
mostly 104 but sometimes it can be down to 100.
# N.B. Different OS kernels have different fs path length limitations
# for it. For Linux, it's 108, for HP-UX it's 92 (or higher) depending
# on its version. For for most of the BSDs (Open, Free, macOS) it's
# mostly 104 but sometimes it can be down to 100.

Ref: https://github.com/aio-libs/aiohttp/issues/3572
"""
# Ref: https://github.com/aio-libs/aiohttp/issues/3572
if not IS_UNIX:
pytest.skip('requires UNIX sockets')

max_sock_len = 92 if IS_HPUX else 108 if IS_LINUX else 100
"""Amount of bytes allocated for the UNIX socket path by OS kernel.
Ref: https://unix.stackexchange.com/a/367012/27133
"""
# Amount of bytes allocated for the UNIX socket path by OS kernel.
# Ref: https://unix.stackexchange.com/a/367012/27133

sock_file_name = 'unix.sock'
unique_prefix = '{!s}-'.format(uuid4())
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for client_exceptions.py"""
# Tests for client_exceptions.py

import errno
import pickle
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_functional.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""HTTP client functional tests against aiohttp.web server"""
# HTTP client functional tests against aiohttp.web server

import asyncio
import datetime
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Tests for aiohttp/client.py"""
# Tests for aiohttp/client.py

import gc
import sys
Expand Down
7 changes: 3 additions & 4 deletions tests/test_client_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,10 @@ async def test_ws_connect_err_challenge(loop, ws_key, key_data) -> None:


async def test_ws_connect_common_headers(ws_key, loop, key_data) -> None:
"""Emulate a headers dict being reused for a second ws_connect.
# Emulate a headers dict being reused for a second ws_connect.

In this scenario, we need to ensure that the newly generated secret key
is sent to the server, not the stale key.
"""
# In this scenario, we need to ensure that the newly generated secret key
# is sent to the server, not the stale key.
headers = {}

async def test_connection() -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests of http client with custom Connector"""
# Tests of http client with custom Connector

import asyncio
import gc
Expand Down Expand Up @@ -28,19 +28,19 @@

@pytest.fixture()
def key():
"""Connection key"""
# Connection key
return ConnectionKey('localhost', 80, False, None, None, None, None)


@pytest.fixture
def key2():
"""Connection key"""
# Connection key
return ConnectionKey('localhost', 80, False, None, None, None, None)


@pytest.fixture
def ssl_key():
"""Connection key"""
# Connection key
return ConnectionKey('localhost', 80, True, None, None, None, None)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def test_invalid_values(self) -> None:
self.assertEqual(cookie["expires"], "")

def test_cookie_not_expired_when_added_after_removal(self) -> None:
"""Test case for https://github.com/aio-libs/aiohttp/issues/2084"""
# Test case for https://github.com/aio-libs/aiohttp/issues/2084
timestamps = [533588.993, 533588.993, 533588.993,
533588.993, 533589.093, 533589.093]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_http_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for http_exceptions.py"""
# Tests for http_exceptions.py

import pickle

Expand Down
10 changes: 5 additions & 5 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for aiohttp/protocol.py"""
# Tests for aiohttp/protocol.py

import asyncio
import zlib
Expand Down Expand Up @@ -41,7 +41,7 @@ def protocol():

@pytest.fixture(params=REQUEST_PARSERS)
def parser(loop, protocol, request):
"""Parser implementations"""
# Parser implementations
return request.param(protocol, loop,
max_line_size=8190,
max_headers=32768,
Expand All @@ -50,13 +50,13 @@ def parser(loop, protocol, request):

@pytest.fixture(params=REQUEST_PARSERS)
def request_cls(request):
"""Request Parser class"""
# Request Parser class
return request.param


@pytest.fixture(params=RESPONSE_PARSERS)
def response(loop, protocol, request):
"""Parser implementations"""
# Parser implementations
return request.param(protocol, loop,
max_line_size=8190,
max_headers=32768,
Expand All @@ -65,7 +65,7 @@ def response(loop, protocol, request):

@pytest.fixture(params=RESPONSE_PARSERS)
def response_cls(request):
"""Parser implementations"""
# Parser implementations
return request.param


Expand Down
2 changes: 1 addition & 1 deletion tests/test_http_writer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for aiohttp/http_writer.py"""
# Tests for aiohttp/http_writer.py
import zlib
from unittest import mock

Expand Down
2 changes: 1 addition & 1 deletion tests/test_locks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests of custom aiohttp locks implementations"""
# Tests of custom aiohttp locks implementations
import asyncio

import pytest
Expand Down
12 changes: 3 additions & 9 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,9 +1264,7 @@ async def test_write_preserves_content_disposition(
assert message == b'foo\r\n--:--\r\n'

async def test_preserve_content_disposition_header(self, buf, stream):
"""
https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
"""
# https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
with pathlib.Path(__file__).open('rb') as fobj:
with aiohttp.MultipartWriter('form-data', boundary=':') as writer:
part = writer.append(
Expand Down Expand Up @@ -1295,9 +1293,7 @@ async def test_preserve_content_disposition_header(self, buf, stream):
)

async def test_set_content_disposition_override(self, buf, stream):
"""
https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
"""
# https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
with pathlib.Path(__file__).open('rb') as fobj:
with aiohttp.MultipartWriter('form-data', boundary=':') as writer:
part = writer.append(
Expand Down Expand Up @@ -1326,9 +1322,7 @@ async def test_set_content_disposition_override(self, buf, stream):
)

async def test_reset_content_disposition_header(self, buf, stream):
"""
https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
"""
# https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381
with pathlib.Path(__file__).open('rb') as fobj:
with aiohttp.MultipartWriter('form-data', boundary=':') as writer:
part = writer.append(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_proxy_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@pytest.fixture
def proxy_test_server(aiohttp_raw_server, loop, monkeypatch):
"""Handle all proxy requests and imitate remote server response."""
# Handle all proxy requests and imitate remote server response.

_patch_ssl_transport(monkeypatch)

Expand Down Expand Up @@ -471,7 +471,7 @@ async def request(pid):


def _patch_ssl_transport(monkeypatch):
"""Make ssl transport substitution to prevent ssl handshake."""
# Make ssl transport substitution to prevent ssl handshake.
def _make_ssl_transport_dummy(self, rawsock, protocol, sslcontext,
waiter=None, **kwargs):
return self._make_socket_transport(rawsock, protocol, waiter,
Expand All @@ -487,7 +487,7 @@ def _make_ssl_transport_dummy(self, rawsock, protocol, sslcontext,


def mock_is_file(self):
""" make real netrc file invisible in home dir """
# make real netrc file invisible in home dir
if self.name in ['_netrc', '.netrc'] and self.parent == self.home():
return False
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async def test_bad() -> None:
if IS_PYPY and bool(os.environ.get('PYTHONASYNCIODEBUG'))
else {'failed': 1, 'passed': 1}
)
"""Under PyPy "coroutine 'foobar' was never awaited" does not happen."""
# Under PyPy "coroutine 'foobar' was never awaited" does not happen.
result.assert_outcomes(**expected_outcomes)


Expand Down
30 changes: 13 additions & 17 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for streams.py"""
# Tests for streams.py

import abc
import asyncio
Expand Down Expand Up @@ -705,8 +705,7 @@ async def test_end_chunk_receiving_without_begin(self) -> None:
stream.end_http_chunk_receiving()

async def test_readchunk_with_unread(self) -> None:
"""Test that stream.unread does not break controlled chunk receiving.
"""
# Test that stream.unread does not break controlled chunk receiving.
stream = self._make_one()

# Send 2 chunks
Expand Down Expand Up @@ -745,9 +744,8 @@ async def test_readchunk_with_unread(self) -> None:
assert not end_of_chunk

async def test_readchunk_with_other_read_calls(self) -> None:
"""Test that stream.readchunk works when other read calls are made on
the stream.
"""
# Test that stream.readchunk works when other read calls are made on
# the stream.
stream = self._make_one()

stream.begin_http_chunk_receiving()
Expand Down Expand Up @@ -782,7 +780,7 @@ async def test_readchunk_with_other_read_calls(self) -> None:
assert not end_of_chunk

async def test_chunksplits_memory_leak(self) -> None:
""" Test for memory leak on chunksplits """
# Test for memory leak on chunksplits
stream = self._make_one()

N = 500
Expand All @@ -806,7 +804,7 @@ async def test_chunksplits_memory_leak(self) -> None:
assert abs(after - before) == 0

async def test_read_empty_chunks(self) -> None:
"""Test that feeding empty chunks does not break stream"""
# Test that feeding empty chunks does not break stream
stream = self._make_one()

# Simulate empty first chunk. This is significant special case
Expand Down Expand Up @@ -835,9 +833,8 @@ async def test_read_empty_chunks(self) -> None:
assert data == b'ungzipped data'

async def test_readchunk_separate_http_chunk_tail(self) -> None:
"""Test that stream.readchunk returns (b'', True) when end of
http chunk received after body
"""
# Test that stream.readchunk returns (b'', True) when end of
# http chunk received after body
loop = asyncio.get_event_loop()
stream = self._make_one()

Expand Down Expand Up @@ -1278,25 +1275,24 @@ async def test_stream_reader_lines() -> None:


async def test_stream_reader_chunks_complete() -> None:
"""Tests if chunked iteration works if the chunking works out
(i.e. the data is divisible by the chunk size)
"""
# Tests if chunked iteration works if the chunking works out
# (i.e. the data is divisible by the chunk size)
chunk_iter = chunkify(DATA, 9)
async for data in (await create_stream()).iter_chunked(9):
assert data == next(chunk_iter, None)
pytest.raises(StopIteration, next, chunk_iter)


async def test_stream_reader_chunks_incomplete() -> None:
"""Tests if chunked iteration works if the last chunk is incomplete"""
# Tests if chunked iteration works if the last chunk is incomplete
chunk_iter = chunkify(DATA, 8)
async for data in (await create_stream()).iter_chunked(8):
assert data == next(chunk_iter, None)
pytest.raises(StopIteration, next, chunk_iter)


async def test_data_queue_empty() -> None:
"""Tests that async looping yields nothing if nothing is there"""
# Tests that async looping yields nothing if nothing is there
loop = asyncio.get_event_loop()
buffer = streams.DataQueue(loop)
buffer.feed_eof()
Expand All @@ -1306,7 +1302,7 @@ async def test_data_queue_empty() -> None:


async def test_data_queue_items() -> None:
"""Tests that async looping yields objects identically"""
# Tests that async looping yields objects identically
loop = asyncio.get_event_loop()
buffer = streams.DataQueue(loop)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ async def make_client():


async def test_aiohttp_client_close_is_idempotent() -> None:
"""
a test client, called multiple times, should
not attempt to close the server again.
"""
# a test client, called multiple times, should
# not attempt to close the server again.
app = _create_example_app()
client = _TestClient(_TestServer(app))
await client.close()
Expand Down
12 changes: 3 additions & 9 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ def test_add_static_append_version_non_exists_file_without_slash(


def test_add_static_append_version_follow_symlink(router, tmp_path) -> None:
"""
Tests the access to a symlink, in static folder with apeend_version
"""
# Tests the access to a symlink, in static folder with apeend_version
symlink_path = tmp_path / 'append_version_symlink'
symlink_target_path = pathlib.Path(__file__).parent
pathlib.Path(str(symlink_path)).symlink_to(str(symlink_target_path), True)
Expand All @@ -442,9 +440,7 @@ def test_add_static_append_version_follow_symlink(router, tmp_path) -> None:

def test_add_static_append_version_not_follow_symlink(router,
tmp_path) -> None:
"""
Tests the access to a symlink, in static folder with apeend_version
"""
# Tests the access to a symlink, in static folder with apeend_version

symlink_path = tmp_path / 'append_version_symlink'
symlink_target_path = pathlib.Path(__file__).parent
Expand Down Expand Up @@ -1238,9 +1234,7 @@ def test_prefixed_subapp_resource_canonical(app) -> None:


async def test_prefixed_subapp_overlap(app) -> None:
"""
Subapp should not overshadow other subapps with overlapping prefixes
"""
# Subapp should not overshadow other subapps with overlapping prefixes
subapp1 = web.Application()
handler1 = make_handler()
subapp1.router.add_get('/a', handler1)
Expand Down
Loading

0 comments on commit 1864880

Please sign in to comment.