Changeset 1663:40280d912474


Ignore:
Timestamp:
Jul 27, 2016, 10:54:56 PM (3 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Remove `unittest` dependency

- Remove `unittest` imports

- Let test case classes inherit from `object` instead of
  `unittest.TestCase`

- Use `setup_method` and `teardown_method` instead of
  `unittest`'s `setUp` and `tearDown`

- Use `pytest.mark.skipif` instead of `unittest.skipIf`

- Use `pytest.mark.slow_test` instead of decorator in
  `test/__init__.py`. Exclude slow tests with

    py.test -m "not slow_test" test

- Replace some leftover `assert*` calls with `assert`
  statements

- Replace `assert False, ...` with `pytest.fail`

ticket: 98
Location:
test
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • test/__init__.py

    r1565 r1663  
    1 """
    2 Setup for unit tests.
    3 """
    4 
    5 from __future__ import unicode_literals
    6 
    7 import os
    8 import unittest
    9 
    10 
    11 __all__ = ["skip_long_running_test"]
    12 
    13 
    14 def skip_long_running_test_condition():
    15     """
    16     Helper function to use with `unittest.skipIf`.
    17 
    18     This requires either Python >= 2.7 or the standalone `unittest2`
    19     module.
    20     """
    21     skip_flag = os.environ.get("SKIP_LONG_RUNNING_TESTS", "0")
    22     return skip_flag == "1"
    23 
    24 
    25 skip_long_running_test = unittest.skipIf(skip_long_running_test_condition(),
    26                                          "skipping long-running test")
  • test/test_error.py

    r1660 r1663  
    77
    88import ftplib
    9 import unittest
    109
    1110import pytest
     
    1413
    1514
    16 class TestFTPErrorArguments(unittest.TestCase):
     15class TestFTPErrorArguments(object):
    1716    """
    1817    The `*Error` constructors should accept either a byte string or a
     
    3130
    3231
    33 class TestErrorConversion(unittest.TestCase):
     32class TestErrorConversion(object):
    3433
    3534    def callee(self):
  • test/test_file.py

    r1661 r1663  
    66
    77import ftplib
    8 import unittest
    98
    109import pytest
     
    4443
    4544
    46 class TestFileOperations(unittest.TestCase):
     45class TestFileOperations(object):
    4746    """Test operations with file-like objects."""
    4847
  • test/test_file_transfer.py

    r1660 r1663  
    77import io
    88import random
    9 import unittest
    109
    1110import pytest
     
    3029
    3130
    32 class TestTimestampComparison(unittest.TestCase):
     31class TestTimestampComparison(object):
    3332
    3433    def test_source_is_newer_than_target(self):
     
    8584
    8685
    87 class TestChunkwiseTransfer(unittest.TestCase):
     86class TestChunkwiseTransfer(object):
    8887
    8988    def _random_string(self, count):
  • test/test_host.py

    r1660 r1663  
    1313import random
    1414import time
    15 import unittest
    1615import warnings
    1716
     
    126125
    127126    def upload(self, source, target, mode=""):
    128         assert False, "`FTPHost.upload` should not have been called"
     127        pytest.fail("`FTPHost.upload` should not have been called")
    129128
    130129    def download(self, source, target, mode=""):
    131         assert False, "`FTPHost.download` should not have been called"
     130        pytest.fail("`FTPHost.download` should not have been called")
    132131
    133132
     
    160159# Test cases
    161160#
    162 class TestInitAndClose(unittest.TestCase):
     161class TestInitAndClose(object):
    163162    """Test initialization and closure of `FTPHost` objects."""
    164163
     
    170169
    171170
    172 class TestLogin(unittest.TestCase):
     171class TestLogin(object):
    173172
    174173    def test_invalid_login(self):
     
    178177
    179178
    180 class TestKeepAlive(unittest.TestCase):
     179class TestKeepAlive(object):
    181180
    182181    def test_succeeding_keep_alive(self):
     
    193192
    194193
    195 class TestSetParser(unittest.TestCase):
     194class TestSetParser(object):
    196195
    197196    class TrivialParser(ftputil.stat.Parser):
     
    225224
    226225
    227 class TestCommandNotImplementedError(unittest.TestCase):
     226class TestCommandNotImplementedError(object):
    228227
    229228    def test_command_not_implemented_error(self):
     
    240239
    241240
    242 class TestRecursiveListingForDotAsPath(unittest.TestCase):
     241class TestRecursiveListingForDotAsPath(object):
    243242    """
    244243    Return a recursive directory listing when the path to list
     
    273272
    274273
    275 class TestUploadAndDownload(unittest.TestCase):
     274class TestUploadAndDownload(object):
    276275    """Test ASCII upload and binary download as examples."""
    277276
     
    374373
    375374
    376 class TestTimeShift(unittest.TestCase):
     375class TestTimeShift(object):
    377376
    378377    def test_rounded_time_shift(self):
     
    468467
    469468
    470 class TestAcceptEitherUnicodeOrBytes(unittest.TestCase):
     469class TestAcceptEitherUnicodeOrBytes(object):
    471470    """
    472471    Test whether certain `FTPHost` methods accept either unicode
     
    474473    """
    475474
    476     def setUp(self):
     475    def setup_method(self, method):
    477476        self.host = test_base.ftp_host_factory()
    478477
     
    585584
    586585
    587 class TestFailingPickling(unittest.TestCase):
     586class TestFailingPickling(object):
    588587
    589588    def test_failing_pickling(self):
  • test/test_path.py

    r1660 r1663  
    88import ftplib
    99import time
    10 import unittest
    1110
    1211import pytest
     
    3534
    3635
    37 class TestPath(unittest.TestCase):
     36class TestPath(object):
    3837    """Test operations in `FTPHost.path`."""
    3938
     
    113112
    114113
    115 class TestAcceptEitherBytesOrUnicode(unittest.TestCase):
    116 
    117     def setUp(self):
     114class TestAcceptEitherBytesOrUnicode(object):
     115
     116    def setup_method(self, method):
    118117        self.host = test_base.ftp_host_factory()
    119118
     
    168167        assert result == "ä/ö/ü"
    169168        #  Need explicit type check for Python 2
    170         self.assertTrue(isinstance(result, ftputil.compat.unicode_type))
     169        assert isinstance(result, ftputil.compat.unicode_type)
    171170        # Only bytes
    172171        parts = [as_bytes(s) for s in "äöü"]
  • test/test_public_servers.py

    r1660 r1663  
    99import socket
    1010import subprocess
    11 import unittest
     11
     12import pytest
    1213
    1314import ftputil
     
    7576
    7677
    77 class TestPublicServers(unittest.TestCase):
     78class TestPublicServers(object):
    7879    """
    7980    Get directory listings from various public FTP servers
     
    160161            host.close()
    161162
    162     @test.skip_long_running_test
     163    @pytest.mark.slow_test
    163164    def test_servers(self):
    164165        """
  • test/test_real_ftp.py

    r1660 r1663  
    2020import os
    2121import time
    22 import unittest
    2322import stat
    2423
     
    102101
    103102
    104 class RealFTPTest(unittest.TestCase):
    105 
    106     def setUp(self):
     103class RealFTPTest(object):
     104
     105    def setup_method(self, method):
    107106        # Server, username, password.
    108107        self.login_data = ("localhost", "ftptest",
     
    111110        self.cleaner = Cleaner(self.host)
    112111
    113     def tearDown(self):
     112    def teardown_method(self, method):
    114113        self.cleaner.clean()
    115114        self.host.close()
     
    147146        non_empty = host.open(file_name, "w")
    148147        non_empty.close()
    149         self.assertRaises(ftputil.error.PermanentError, host.rmdir, dir_name)
     148        with pytest.raises(ftputil.error.PermanentError):
     149            host.rmdir(dir_name)
    150150        # Remove file.
    151151        host.unlink(file_name)
     
    155155                host.remove(dir_name)
    156156            except ftputil.error.PermanentError as exc:
    157                 self.assertTrue(str(exc).startswith(
    158                                 "remove/unlink can only delete files"))
     157                assert str(exc).startswith(
     158                         "remove/unlink can only delete files")
    159159            else:
    160                 self.assertTrue(False, "we shouldn't have come here")
     160                pytest.fail("we shouldn't have come here")
    161161        finally:
    162162            # Delete empty directory.
     
    168168        host = self.host
    169169        # No `_dir1_` yet
    170         self.assertFalse("_dir1_" in host.listdir(host.curdir))
     170        assert "_dir1_" not in host.listdir(host.curdir)
    171171        # Vanilla case, all should go well.
    172172        host.makedirs("_dir1_/dir2/dir3/dir4")
     
    284284        # Try to remove a file as root "directory".
    285285        host.rmtree("_dir1_/file1", ignore_errors=True, onerror=error_handler)
    286         self.assertEqual(log, [])
     286        assert log == []
    287287        host.rmtree("_dir1_/file1", ignore_errors=False, onerror=error_handler)
    288288        assert log[0][0] == host.listdir
     
    582582        assert self.host.time_shift() == EXPECTED_TIME_SHIFT
    583583
    584     @test.skip_long_running_test
     584    @pytest.mark.slow_test
    585585    def test_upload(self):
    586586        host = self.host
     
    608608            os.unlink(local_file)
    609609
    610     @test.skip_long_running_test
     610    @pytest.mark.slow_test
    611611    def test_download(self):
    612612        host = self.host
     
    797797    TEST_FILE_NAME = "rest_test"
    798798
    799     def setUp(self):
    800         super(TestRestArgument, self).setUp()
     799    def setup_method(self, method):
     800        super(TestRestArgument, self).setup_method(method)
    801801        # Write test file.
    802802        with self.host.open(self.TEST_FILE_NAME, "wb") as fobj:
     
    968968        assert not objects_after_test - objects_before_test
    969969
    970     @unittest.skipIf(ftputil.compat.python_version > 2,
    971                      "test requires M2Crypto which only works on Python 2")
     970    @pytest.mark.skipif(
     971      ftputil.compat.python_version > 2,
     972      reason="test requires M2Crypto which only works on Python 2")
    972973    def test_m2crypto_session(self):
    973974        """
  • test/test_session.py

    r1662 r1663  
    88
    99from __future__ import unicode_literals
    10 
    11 import unittest
    1210
    1311import ftputil.session
     
    5149
    5250
    53 class TestSessionFactory(unittest.TestCase):
     51class TestSessionFactory(object):
    5452    """
    5553    Test if session factories created by
  • test/test_stat.py

    r1660 r1663  
    99import stat
    1010import time
    11 import unittest
    1211
    1312import pytest
     
    5049
    5150
    52 class TestParsers(unittest.TestCase):
     51class TestParsers(object):
    5352
    5453    #
     
    353352
    354353
    355 class TestLstatAndStat(unittest.TestCase):
     354class TestLstatAndStat(object):
    356355    """
    357356    Test `FTPHost.lstat` and `FTPHost.stat` (test currently only
     
    359358    """
    360359
    361     def setUp(self):
     360    def setup_method(self, method):
    362361        # Most tests in this class need the mock session class with
    363362        # Unix format, so make this the default. Tests which need
     
    516515
    517516
    518 class TestListdir(unittest.TestCase):
     517class TestListdir(object):
    519518    """Test `FTPHost.listdir`."""
    520519
    521     def setUp(self):
     520    def setup_method(self, method):
    522521        self.stat = \
    523522          _test_stat(session_factory=mock_ftplib.MockUnixFormatSession)
  • test/test_stat_cache.py

    r1660 r1663  
    66
    77import time
    8 import unittest
    98
    109import pytest
     
    1615
    1716
    18 class TestStatCache(unittest.TestCase):
     17class TestStatCache(object):
    1918
    20     def setUp(self):
     19    def setup_method(self, method):
    2120        self.cache = ftputil.stat_cache.StatCache()
    2221
     
    4645
    4746    def test_len(self):
    48         self.assertEqual(len(self.cache), 0)
     47        assert len(self.cache) == 0
    4948        self.cache["/path1"] = "test1"
    5049        self.cache["/path2"] = "test2"
  • test/test_sync.py

    r1660 r1663  
    1010import os
    1111import shutil
    12 import unittest
    1312
    1413import ftputil
     
    2019
    2120
    22 class TestLocalToLocal(unittest.TestCase):
     21class TestLocalToLocal(object):
    2322
    24     def setUp(self):
     23    def setup_method(self, method):
    2524        if not os.path.exists("test_empty"):
    2625            os.mkdir("test_empty")
     
    117116
    118117
    119 class TestUploadFromWindows(unittest.TestCase):
     118class TestUploadFromWindows(object):
    120119
    121120    def test_no_mixed_separators(self):
  • test/test_tool.py

    r1660 r1663  
    55from __future__ import unicode_literals
    66
    7 import unittest
    8 
    97import ftputil.compat as compat
    108import ftputil.tool
    119
    1210
    13 class TestSameStringTypeAs(unittest.TestCase):
     11class TestSameStringTypeAs(object):
    1412
    1513    # The first check for equality is enough for Python 3, where
     
    3836
    3937
    40 class TestSimpleConversions(unittest.TestCase):
     38class TestSimpleConversions(object):
    4139
    4240    def test_as_bytes(self):
     
    5755
    5856
    59 class TestEncodeIfUnicode(unittest.TestCase):
     57class TestEncodeIfUnicode(object):
    6058
    6159    def test_do_encode(self):
  • test/test_with_statement.py

    r1660 r1663  
    44
    55from __future__ import unicode_literals
    6 
    7 import unittest
    86
    97import pytest
     
    2523# Test cases
    2624#
    27 class TestHostContextManager(unittest.TestCase):
     25class TestHostContextManager(object):
    2826
    2927    def test_normal_operation(self):
     
    4947            assert host.closed is True
    5048        else:
    51             assert False, "`ClientCodeException` not raised"
     49            pytest.fail("`ClientCodeException` not raised")
    5250
    5351
    54 class TestFileContextManager(unittest.TestCase):
     52class TestFileContextManager(object):
    5553
    5654    def test_normal_operation(self):
     
    8684                assert fobj.closed is True
    8785            else:
    88                 assert False, "`ClientCodeException` not raised"
     86                pytest.fail("`ClientCodeException` not raised")
Note: See TracChangeset for help on using the changeset viewer.