Changeset 1647:039a9b8f04a7


Ignore:
Timestamp:
Jul 26, 2016, 6:34:34 PM (4 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
histedit_source:
7f9a5a643db57b594e27bc2fa3e677ff2998e489
Message:
Convert `test/test_file.py` to use pytest

ticket: 98
File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/test_file.py

    r1564 r1647  
    1 # Copyright (C) 2002-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2002-2016, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# and ftputil contributors (see `doc/contributors.txt`)
    33# See the file LICENSE for licensing terms.
     
    77import ftplib
    88import unittest
     9
     10import pytest
    911
    1012import ftputil.compat
     
    5557        """Test whether `FTPFile` cache of `FTPHost` object works."""
    5658        host = test_base.ftp_host_factory()
    57         self.assertEqual(len(host._children), 0)
     59        assert len(host._children) == 0
    5860        path1 = "path1"
    5961        path2 = "path2"
     
    6163        file1 = host.open(path1, "w")
    6264        child1 = host._children[0]
    63         self.assertEqual(len(host._children), 1)
    64         self.assertFalse(child1._file.closed)
     65        assert len(host._children) == 1
     66        assert not child1._file.closed
    6567        # Open another file.
    6668        file2 = host.open(path2, "w")
    6769        child2 = host._children[1]
    68         self.assertEqual(len(host._children), 2)
    69         self.assertFalse(child2._file.closed)
     70        assert len(host._children) == 2
     71        assert not child2._file.closed
    7072        # Close first file.
    7173        file1.close()
    72         self.assertEqual(len(host._children), 2)
    73         self.assertTrue(child1._file.closed)
    74         self.assertFalse(child2._file.closed)
     74        assert len(host._children) == 2
     75        assert child1._file.closed
     76        assert not child2._file.closed
    7577        # Re-open first child's file.
    7678        file1 = host.open(path1, "w")
    7779        child1_1 = file1._host
    7880        # Check if it's reused.
    79         self.assertTrue(child1 is child1_1)
    80         self.assertFalse(child1._file.closed)
    81         self.assertFalse(child2._file.closed)
     81        assert child1 is child1_1
     82        assert not child1._file.closed
     83        assert not child2._file.closed
    8284        # Close second file.
    8385        file2.close()
    84         self.assertTrue(child2._file.closed)
     86        assert child2._file.closed
    8587
    8688    def test_write_to_directory(self):
    8789        """Test whether attempting to write to a directory fails."""
    8890        host = test_base.ftp_host_factory()
    89         self.assertRaises(ftputil.error.FTPIOError, host.open,
    90                           "/home/sschwarzer", "w")
     91        with pytest.raises(ftputil.error.FTPIOError):
     92            host.open("/home/sschwarzer", "w")
    9193
    9294    def test_binary_read(self):
     
    9597        with host.open("some_file", "rb") as fobj:
    9698            data = fobj.read()
    97         self.assertEqual(data, ReadMockSession.mock_file_content)
     99        assert data == ReadMockSession.mock_file_content
    98100
    99101    def test_binary_write(self):
     
    105107        child_data = mock_ftplib.content_of("dummy")
    106108        expected_data = data
    107         self.assertEqual(child_data, expected_data)
     109        assert child_data == expected_data
    108110
    109111    def test_ascii_read(self):
     
    112114        with host.open("dummy", "r") as input_:
    113115            data = input_.read(0)
    114             self.assertEqual(data, "")
     116            assert data == ""
    115117            data = input_.read(3)
    116             self.assertEqual(data, "lin")
     118            assert data == "lin"
    117119            data = input_.read(7)
    118             self.assertEqual(data, "e 1\nano")
     120            assert data == "e 1\nano"
    119121            data = input_.read()
    120             self.assertEqual(data, "ther line\nyet another line")
     122            assert data == "ther line\nyet another line"
    121123            data = input_.read()
    122             self.assertEqual(data, "")
     124            assert data == ""
    123125
    124126    def test_ascii_write(self):
     
    131133        # This corresponds to the byte stream, so expect a `bytes` object.
    132134        expected_data = b" \r\nline 2\r\nline 3"
    133         self.assertEqual(child_data, expected_data)
     135        assert child_data == expected_data
    134136
    135137    # TODO: Add tests with given encoding and possibly buffering.
     
    145147        child_data = mock_ftplib.content_of("dummy")
    146148        expected_data = b" \r\nline 2\r\nline 3"
    147         self.assertEqual(child_data, expected_data)
     149        assert child_data == expected_data
    148150        # Ensure that the original data was not modified.
    149         self.assertEqual(data, backup_data)
     151        assert data == backup_data
    150152
    151153    def test_binary_readline(self):
     
    154156        input_ = host.open("dummy", "rb")
    155157        data = input_.readline(3)
    156         self.assertEqual(data, b"lin")
     158        assert data == b"lin"
    157159        data = input_.readline(10)
    158         self.assertEqual(data, b"e 1\r\n")
     160        assert data == b"e 1\r\n"
    159161        data = input_.readline(13)
    160         self.assertEqual(data, b"another line\r")
    161         data = input_.readline()
    162         self.assertEqual(data, b"\n")
    163         data = input_.readline()
    164         self.assertEqual(data, b"yet another line")
    165         data = input_.readline()
    166         self.assertEqual(data, b"")
     162        assert data == b"another line\r"
     163        data = input_.readline()
     164        assert data == b"\n"
     165        data = input_.readline()
     166        assert data == b"yet another line"
     167        data = input_.readline()
     168        assert data == b""
    167169        input_.close()
    168170
     
    172174        input_ = host.open("dummy", "r")
    173175        data = input_.readline(3)
    174         self.assertEqual(data, "lin")
     176        assert data == "lin"
    175177        data = input_.readline(10)
    176         self.assertEqual(data, "e 1\n")
     178        assert data == "e 1\n"
    177179        data = input_.readline(13)
    178         self.assertEqual(data, "another line\n")
    179         data = input_.readline()
    180         self.assertEqual(data, "yet another line")
    181         data = input_.readline()
    182         self.assertEqual(data, "")
     180        assert data == "another line\n"
     181        data = input_.readline()
     182        assert data == "yet another line"
     183        data = input_.readline()
     184        assert data == ""
    183185        input_.close()
    184186
     
    188190        input_ = host.open("dummy", "r")
    189191        data = input_.read(3)
    190         self.assertEqual(data, "lin")
     192        assert data == "lin"
    191193        data = input_.readlines()
    192         self.assertEqual(data, ["e 1\n", "another line\n",
    193                                 "yet another line"])
     194        assert data == ["e 1\n", "another line\n", "yet another line"]
    194195        input_.close()
    195196
     
    202203        input_ = host.open("dummy", "rb")
    203204        input_iterator = iter(input_)
    204         self.assertEqual(next(input_iterator), b"line 1\r\n")
    205         self.assertEqual(next(input_iterator), b"another line\r\n")
    206         self.assertEqual(next(input_iterator), b"yet another line")
    207         self.assertRaises(StopIteration, input_iterator.__next__)
     205        assert next(input_iterator) == b"line 1\r\n"
     206        assert next(input_iterator) == b"another line\r\n"
     207        assert next(input_iterator) == b"yet another line"
     208        with pytest.raises(StopIteration):
     209            input_iterator.__next__()
    208210        input_.close()
    209211
     
    216218        input_ = host.open("dummy")
    217219        input_iterator = iter(input_)
    218         self.assertEqual(next(input_iterator), "line 1\n")
    219         self.assertEqual(next(input_iterator), "another line\n")
    220         self.assertEqual(next(input_iterator), "yet another line")
    221         self.assertRaises(StopIteration, input_iterator.__next__)
     220        assert next(input_iterator) == "line 1\n"
     221        assert next(input_iterator) == "another line\n"
     222        assert next(input_iterator) == "yet another line"
     223        with pytest.raises(StopIteration):
     224            input_iterator.__next__()
    222225        input_.close()
    223226
     
    225228        """Test whether reading a file which isn't there fails."""
    226229        host = test_base.ftp_host_factory()
    227         self.assertRaises(ftputil.error.FTPIOError, host.open, "notthere", "r")
     230        with pytest.raises(ftputil.error.FTPIOError):
     231            host.open("notthere", "r")
    228232
    229233
Note: See TracChangeset for help on using the changeset viewer.