Changeset 1646:f2273a0e1ea9


Ignore:
Timestamp:
Jul 24, 2016, 11:28:46 PM (4 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
histedit_source:
9bce2a71ce13081c320cc075452a2c7f03f1523d
rebase_source:
0500358a9631f9b3890ad6148f4d5bf8d5f473c1
Message:
Use `pytest.raises` for exception checks

ticket: 98
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_error.py

    r1645 r1646  
    88import ftplib
    99import unittest
     10
     11import pytest
    1012
    1113import ftputil.error
     
    3941        argument.
    4042        """
    41         try:
     43        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
    4244            with ftputil.error.ftplib_error_to_ftp_os_error:
    4345                self.callee()
    44         except ftputil.error.FTPOSError as exc:
    45             assert not (exc.args and
    46                         isinstance(exc.args[0], ftplib.error_perm))
    47         else:
    48             # We shouldn't come here.
    49             assert False
     46        exc = exc_info.value
     47        assert not (exc.args and
     48                    isinstance(exc.args[0], ftplib.error_perm))
     49        del exc_info
    5050
    5151    def test_ftplib_error_to_ftp_os_error_non_ascii_server_message(self):
     
    5858          ftputil.tool.as_bytes(
    5959            "Não é possível criar um arquivo já existente.")
    60         try:
     60        with pytest.raises(ftputil.error.PermanentError):
    6161            with ftputil.error.ftplib_error_to_ftp_os_error:
    6262                raise ftplib.error_perm(message)
    63         # We expect a `PermanentError`.
    64         except ftputil.error.PermanentError:
    65             pass
    66         except UnicodeDecodeError:
    67             assert False
    6863
    6964    def test_ftplib_error_to_ftp_io_error(self):
     
    7267        argument.
    7368        """
    74         try:
     69        with pytest.raises(ftputil.error.FTPIOError) as exc_info:
    7570            with ftputil.error.ftplib_error_to_ftp_io_error:
    7671                self.callee()
    77         except ftputil.error.FTPIOError as exc:
    78             assert not (exc.args and
    79                         isinstance(exc.args[0], ftplib.error_perm))
    80         else:
    81             # We shouldn't come here.
    82             assert False
     72        exc = exc_info.value
     73        assert not (exc.args and
     74                    isinstance(exc.args[0], ftplib.error_perm))
     75        del exc_info
    8376
    8477    def test_error_message_reuse(self):
     
    8881        """
    8982        # See ticket #76.
    90         try:
     83        with pytest.raises(ftputil.error.FTPOSError) as exc_info:
    9184            # Format "host:port" doesn't work.
    9285            host = ftputil.FTPHost("localhost:21", "", "")
    93         except ftputil.error.FTPOSError as exc:
    94             # The error message may be different for different Python
    95             # versions.
    96             assert (
    97               "No address associated with hostname" in str(exc) or
    98               "Name or service not known" in str(exc))
     86        exc = exc_info.value
     87        # The error message may be different for different Python
     88        # versions.
     89        assert (
     90          "No address associated with hostname" in str(exc) or
     91          "Name or service not known" in str(exc))
     92        del exc_info
    9993
    10094
Note: See TracChangeset for help on using the changeset viewer.