From 27faf06327e7270093606de5ccceb5f3314701bc Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Fri, 25 Apr 2014 11:39:59 +0200 Subject: [PATCH] Removed last dependencies on unittest. All tests are pytest-only. --- tests/test_auth.py | 4 +--- tests/test_binary.py | 6 ++---- tests/test_cli.py | 27 ++++++++++++++------------- tests/test_defaults.py | 6 ++---- tests/test_docs.py | 2 +- tests/test_downloads.py | 27 ++++++++++++--------------- tests/test_exit_status.py | 4 +--- tests/test_httpie.py | 4 +--- tests/test_output.py | 10 ++++------ tests/test_sessions.py | 8 ++++---- tests/test_stream.py | 4 +--- tests/test_uploads.py | 5 ++--- tests/test_windows.py | 13 +++++++------ 13 files changed, 52 insertions(+), 68 deletions(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index 1f405419..bb602e5c 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,6 +1,4 @@ """HTTP authentication-related tests.""" -from unittest import TestCase - import requests import pytest @@ -8,7 +6,7 @@ from tests import http, httpbin, HTTP_OK import httpie.input -class AuthTest(TestCase): +class TestAuth: def test_basic_auth(self): r = http('--auth=user:password', 'GET', httpbin('/basic-auth/user/password')) diff --git a/tests/test_binary.py b/tests/test_binary.py index dc3ccf0a..2f61c692 100644 --- a/tests/test_binary.py +++ b/tests/test_binary.py @@ -1,13 +1,11 @@ """Tests for dealing with binary request and response data.""" -from unittest import TestCase - from httpie.compat import urlopen from httpie.output import BINARY_SUPPRESSED_NOTICE from tests import TestEnvironment, http, httpbin from tests.fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG -class BinaryRequestDataTest(TestCase): +class TestBinaryRequestData: def test_binary_stdin(self): with open(BIN_FILE_PATH, 'rb') as stdin: env = TestEnvironment( @@ -31,7 +29,7 @@ class BinaryRequestDataTest(TestCase): assert bytes(BIN_FILE_CONTENT) in bytes(r) -class BinaryResponseDataTest(TestCase): +class TestBinaryResponseData: url = 'http://www.google.com/favicon.ico' @property diff --git a/tests/test_cli.py b/tests/test_cli.py index f9e057ee..a5e6c7d4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,10 +1,10 @@ """CLI argument parsing related tests.""" import json -from unittest import TestCase - # noinspection PyCompatibility import argparse +import pytest + from httpie import input from httpie.input import KeyValue, KeyValueArgType from httpie import ExitStatus @@ -16,15 +16,15 @@ from tests.fixtures import ( ) -class ItemParsingTest(TestCase): - def setUp(self): - self.key_value_type = KeyValueArgType(*input.SEP_GROUP_ALL_ITEMS) +class TestItemParsing: + + key_value_type = KeyValueArgType(*input.SEP_GROUP_ALL_ITEMS) def test_invalid_items(self): items = ['no-separator'] for item in items: - self.assertRaises(argparse.ArgumentTypeError, - lambda: self.key_value_type(item)) + pytest.raises(argparse.ArgumentTypeError, + self.key_value_type, item) def test_escape(self): headers, data, files, params = input.parse_items([ @@ -93,7 +93,7 @@ class ItemParsingTest(TestCase): assert files['file'][1].read().strip().decode('utf8') == FILE_CONTENT -class QuerystringTest(TestCase): +class TestQuerystring: def test_query_string_params_in_url(self): r = http('--print=Hhb', 'GET', httpbin('/get?a=1&b=2')) path = '/get?a=1&b=2' @@ -120,7 +120,7 @@ class QuerystringTest(TestCase): assert '"url": "%s"' % url in r -class CLIParserTestCase(TestCase): +class TestCLIParser: def test_expand_localhost_shorthand(self): args = parser.parse_args(args=[':'], env=TestEnvironment()) assert args.url == 'http://localhost' @@ -164,8 +164,9 @@ class CLIParserTestCase(TestCase): assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001' -class ArgumentParserTestCase(TestCase): - def setUp(self): +class TestArgumentParser: + + def setup_method(self, method): self.parser = input.Parser() def test_guess_when_method_set_and_valid(self): @@ -256,7 +257,7 @@ class ArgumentParserTestCase(TestCase): ] -class TestNoOptions(TestCase): +class TestNoOptions: def test_valid_no_options(self): r = http('--verbose', '--no-verbose', 'GET', httpbin('/get')) assert 'GET /get HTTP/1.1' not in r @@ -268,7 +269,7 @@ class TestNoOptions(TestCase): assert 'GET /get HTTP/1.1' not in r -class IgnoreStdinTest(TestCase): +class TestIgnoreStdin: def test_ignore_stdin(self): with open(FILE_PATH) as f: env = TestEnvironment(stdin=f, stdin_isatty=False) diff --git a/tests/test_defaults.py b/tests/test_defaults.py index 276b5475..53d14eaa 100644 --- a/tests/test_defaults.py +++ b/tests/test_defaults.py @@ -2,13 +2,11 @@ Tests for the provided defaults regarding HTTP method, and --json vs. --form. """ -from unittest import TestCase - from tests import TestEnvironment, http, httpbin, HTTP_OK from tests.fixtures import FILE_PATH -class ImplicitHTTPMethodTest(TestCase): +class TestImplicitHTTPMethod: def test_implicit_GET(self): r = http(httpbin('/get')) assert HTTP_OK in r @@ -35,7 +33,7 @@ class ImplicitHTTPMethodTest(TestCase): assert HTTP_OK in r -class AutoContentTypeAndAcceptHeadersTest(TestCase): +class TestAutoContentTypeAndAcceptHeaders: """ Test that Accept and Content-Type correctly defaults to JSON, but can still be overridden. The same with Content-Type when --form diff --git a/tests/test_docs.py b/tests/test_docs.py index 8038ecc0..4cd52881 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -30,7 +30,7 @@ assert filenames @pytest.mark.skipif(not has_docutils(), reason='docutils not installed') @pytest.mark.parametrize('filename', filenames) -def test_rst_files_syntax(filename): +def test_rst_file_syntax(filename): p = subprocess.Popen( ['rst2pseudoxml.py', '--report=1', '--exit-status=1', filename], stderr=subprocess.PIPE, diff --git a/tests/test_downloads.py b/tests/test_downloads.py index 37b25778..321541c3 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -1,6 +1,5 @@ import os import time -from unittest import TestCase import pytest from requests.structures import CaseInsensitiveDict @@ -21,7 +20,7 @@ class Response(object): self.status_code = status_code -class DownloadUtilsTest(TestCase): +class TestDownloadUtils: def test_Content_Range_parsing(self): parse = parse_content_range @@ -46,18 +45,16 @@ class DownloadUtilsTest(TestCase): # invalid byte-range-resp-spec pytest.raises(ContentRangeError, parse, 'bytes 100-100/*', 100) - def test_Content_Disposition_parsing(self): - parse = filename_from_content_disposition - assert 'hello-WORLD_123.txt' == parse( - 'attachment; filename=hello-WORLD_123.txt') - assert 'hello-WORLD_123.txt' == parse( - 'attachment; filename=".hello-WORLD_123.txt"') - assert 'white space.txt' == parse( - 'attachment; filename="white space.txt"') - assert '"quotes".txt' == parse( - r'attachment; filename="\"quotes\".txt"') - assert parse('attachment; filename=/etc/hosts') == 'hosts' - assert parse('attachment; filename=') is None + @pytest.mark.parametrize('header, expected_filename', [ + ('attachment; filename=hello-WORLD_123.txt', 'hello-WORLD_123.txt'), + ('attachment; filename=".hello-WORLD_123.txt"', 'hello-WORLD_123.txt'), + ('attachment; filename="white space.txt"', 'white space.txt'), + (r'attachment; filename="\"quotes\".txt"', '"quotes".txt'), + ('attachment; filename=/etc/hosts', 'hosts'), + ('attachment; filename=', None) + ]) + def test_Content_Disposition_parsing(self, header, expected_filename): + assert filename_from_content_disposition(header) == expected_filename def test_filename_from_url(self): assert 'foo.txt' == filename_from_url( @@ -94,7 +91,7 @@ class DownloadUtilsTest(TestCase): assert 'foo.bar-10' == get_unique_filename('foo.bar', attempts(10)) -class DownloadsTest(TestCase): +class TestDownloads: # TODO: more tests def test_actual_download(self): diff --git a/tests/test_exit_status.py b/tests/test_exit_status.py index 2dac02e0..ab66d964 100644 --- a/tests/test_exit_status.py +++ b/tests/test_exit_status.py @@ -1,5 +1,3 @@ -from unittest import TestCase - import requests import pytest @@ -7,7 +5,7 @@ from httpie import ExitStatus from tests import TestEnvironment, http, httpbin, HTTP_OK -class ExitStatusTest(TestCase): +class TestExitStatus: def test_ok_response_exits_0(self): r = http('GET', httpbin('/status/200')) assert HTTP_OK in r diff --git a/tests/test_httpie.py b/tests/test_httpie.py index d855fc4d..8a4fb663 100644 --- a/tests/test_httpie.py +++ b/tests/test_httpie.py @@ -1,12 +1,10 @@ """High-level tests.""" -from unittest import TestCase - from tests import TestEnvironment, http, httpbin, HTTP_OK from tests.fixtures import FILE_PATH, FILE_CONTENT import httpie -class HTTPieTest(TestCase): +class TestHTTPie: def test_debug(self): r = http('--debug') diff --git a/tests/test_output.py b/tests/test_output.py index 4d362267..853556c5 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -1,10 +1,8 @@ -from unittest import TestCase - from httpie import ExitStatus from tests import TestEnvironment, http, httpbin, HTTP_OK, COLOR, CRLF -class VerboseFlagTest(TestCase): +class TestVerboseFlag: def test_verbose(self): r = http('--verbose', 'GET', httpbin('/get'), 'test-header:__test__') assert HTTP_OK in r @@ -24,7 +22,7 @@ class VerboseFlagTest(TestCase): assert r'\"baz\": \"bar\"' in r # response -class PrettyOptionsTest(TestCase): +class TestPrettyOptions: """Test the --pretty flag handling.""" def test_pretty_enabled_by_default(self): @@ -72,7 +70,7 @@ class PrettyOptionsTest(TestCase): assert COLOR not in r -class LineEndingsTest(TestCase): +class TestLineEndings: """ Test that CRLF is properly used in headers and as the headers/body separator. @@ -85,7 +83,7 @@ class LineEndingsTest(TestCase): break assert header.endswith(CRLF), repr(header) else: - self.fail('CRLF between headers and body not found in %r' % msg) + assert 0, 'CRLF between headers and body not found in %r' % msg body = ''.join(lines) assert CRLF not in body return body diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 05f78919..27ac50a0 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -1,16 +1,16 @@ import os import shutil -from unittest import TestCase from tests import TestEnvironment, mk_config_dir, http, httpbin, HTTP_OK -class SessionsTest(TestCase): +class TestSessions: + @property def env(self): return TestEnvironment(config_dir=self.config_dir) - def setUp(self): + def setup_method(self, method): # Start a full-blown session with a custom request header, # authorization, and response cookies. self.config_dir = mk_config_dir() @@ -19,7 +19,7 @@ class SessionsTest(TestCase): env=self.env) assert HTTP_OK in r - def tearDown(self): + def teardown_method(self, method): shutil.rmtree(self.config_dir) def test_session_create(self): diff --git a/tests/test_stream.py b/tests/test_stream.py index c2eafc3a..a8e8b21e 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -1,5 +1,3 @@ -from unittest import TestCase - import pytest from httpie.compat import is_windows @@ -8,7 +6,7 @@ from tests import http, httpbin, TestEnvironment from tests.fixtures import BIN_FILE_CONTENT, BIN_FILE_PATH -class StreamTest(TestCase): +class TestStream: # GET because httpbin 500s with binary POST body. @pytest.mark.skipif(is_windows, diff --git a/tests/test_uploads.py b/tests/test_uploads.py index 0a1df331..2c2fabf9 100644 --- a/tests/test_uploads.py +++ b/tests/test_uploads.py @@ -1,5 +1,4 @@ import os -from unittest import TestCase import pytest @@ -8,7 +7,7 @@ from tests import TestEnvironment, http, httpbin, HTTP_OK from tests.fixtures import FILE_PATH_ARG, FILE_PATH, FILE_CONTENT -class MultipartFormDataFileUploadTest(TestCase): +class TestMultipartFormDataFileUpload: def test_non_existent_file_raises_parse_error(self): with pytest.raises(ParseError): http('--form', 'POST', httpbin('/post'), 'foo@/__does_not_exist__') @@ -24,7 +23,7 @@ class MultipartFormDataFileUploadTest(TestCase): assert '"foo": "bar"' in r -class RequestBodyFromFilePathTest(TestCase): +class TestRequestBodyFromFilePath: """ `http URL @file' diff --git a/tests/test_windows.py b/tests/test_windows.py index bb7c962e..326e6772 100644 --- a/tests/test_windows.py +++ b/tests/test_windows.py @@ -1,6 +1,5 @@ import os import tempfile -from unittest import TestCase import pytest @@ -8,17 +7,19 @@ from tests import TestEnvironment, http, httpbin, Environment from httpie.compat import is_windows -class WindowsOnlyTests(TestCase): - @pytest.mark.skipif(not is_windows, reason='windows-only') +@pytest.mark.skipif(not is_windows, reason='windows-only') +class TestWindowsOnly: + def test_windows_colorized_output(self): # Spits out the colorized output. http(httpbin('/get'), env=Environment()) -class FakeWindowsTest(TestCase): +class TestFakeWindows: def test_output_file_pretty_not_allowed_on_windows(self): env = TestEnvironment(is_windows=True) - r = http('--output', - os.path.join(tempfile.gettempdir(), '__httpie_test_output__'), + output_file = os.path.join( + tempfile.gettempdir(), '__httpie_test_output__') + r = http('--output', output_file, '--pretty=all', 'GET', httpbin('/get'), env=env) assert 'Only terminal output can be colorized on Windows' in r.stderr