Skip to content

Commit 69569a8

Browse files
committed
migrates existing tests from unittest to pytest
1 parent 0ae4cc2 commit 69569a8

9 files changed

+116
-142
lines changed

data/docker/Dockerfile.archlinux

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN pacman -Sy
44

55
# prep/test dependencies
66
RUN pacman -S --noconfirm \
7-
base-devel openssh git python python-pip python-ddt
7+
base-devel openssh git python python-pip python-pytest
88

99
# dups system dependencies
1010
RUN pacman -S --noconfirm \

data/docker/Dockerfile.bionic

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN apt-get update
44

55
# prep/test dependencies
66
RUN apt-get install -y \
7-
openssh-server python3 python3-pip python3-ddt
7+
openssh-server python3 python3-pip python3-pytest
88

99
# Debian build dependencies
1010
RUN apt-get install -y \

data/docker/Dockerfile.stretch

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN apt-get update
44

55
# prep/test dependencies
66
RUN apt-get install -y \
7-
openssh-server python3 python3-pip python3-ddt
7+
openssh-server python3 python3-pip python3-pytest
88

99
# Debian build dependencies
1010
RUN apt-get install -y \

data/scripts/ci/test-dups.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ chown -R dups:dups /home/dups/source
1515

1616
# Run all unittests
1717
cd /home/dups/source
18-
su dups -c "${PYTHON} -m unittest discover -v -s tests"
18+
su dups -c "${PYTHON} -m pytest -vv tests"

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ dbus-python # Daemon support
77
pygobject # Desktop notification support
88

99
# Unittests
10-
ddt
10+
pytest

tests/test_backup.py

+37-44
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
import os
44
import shutil
5-
import unittest
65

76
from dups import backup, exceptions, utils
87

8+
import pytest
99
import utils as test_utils
10-
from ddt import data, ddt
1110

1211

13-
@ddt
14-
class Test_Backup(unittest.TestCase):
12+
class Test_Backup:
1513
missing_backup = '19900100000000'
1614
valid_backup = '19900101000000'
1715
invalid_backup = '19900102000000'
1816

19-
def tearDown(self):
17+
def teardown_method(self, method):
2018
for dir_ in os.listdir(context.BACKUP_DIR):
2119
if dir_ in [self.valid_backup, self.invalid_backup]:
2220
continue
@@ -41,7 +39,7 @@ def get_invalid(self, io):
4139
return backup.Backup.from_name(io, self.invalid_backup,
4240
context.BACKUP_DIR)
4341

44-
@data('local', 'remote')
42+
@pytest.mark.parametrize('target', ['local', 'remote'])
4543
def test_new(self, target):
4644
io = self.get_io(target)
4745

@@ -53,7 +51,7 @@ def test_new(self, target):
5351

5452
io.close()
5553

56-
@data('local', 'remote')
54+
@pytest.mark.parametrize('target', ['local', 'remote'])
5755
def test_from_label(self, target):
5856
io = self.get_io(target)
5957

@@ -62,53 +60,52 @@ def test_from_label(self, target):
6260
self.get_valid(io)
6361

6462
except Exception as e:
65-
self.fail(str(e))
63+
pytest.fail(str(e))
6664

67-
self.assertRaises(exceptions.BackupNotFoundException,
68-
backup.Backup.from_name, io, self.missing_backup,
69-
context.BACKUP_DIR)
65+
with pytest.raises(exceptions.BackupNotFoundException):
66+
backup.Backup.from_name(io, self.missing_backup,
67+
context.BACKUP_DIR)
7068

7169
io.close()
7270

73-
@data('local', 'remote')
71+
@pytest.mark.parametrize('target', ['local', 'remote'])
7472
def test_all_backups(self, target):
7573
io = self.get_io(target)
7674

7775
# Get a list of valid backups
7876
backups = backup.Backup.all_backups(io, context.BACKUP_DIR)
79-
self.assertCountEqual([self.get_valid(io)], backups)
77+
assert len(backups) == 1
8078

8179
# Get a list of all (valid and invalid) backups
8280
backups = backup.Backup.all_backups(io, context.BACKUP_DIR, True, True)
83-
self.assertCountEqual(
84-
[self.get_valid(io), self.get_invalid(io)], backups)
81+
assert len(backups) == 2
8582

8683
io.close()
8784

88-
@data('local', 'remote')
85+
@pytest.mark.parametrize('target', ['local', 'remote'])
8986
def test_latest(self, target):
9087
io = self.get_io(target)
9188

9289
# Get the latest valid backup
9390
bak = backup.Backup.latest(io, context.BACKUP_DIR)
94-
self.assertEqual(self.get_valid(io), bak)
91+
assert self.get_valid(io) == bak
9592

9693
# Get the latest backup including invalid ones
9794
bak = backup.Backup.latest(io, context.BACKUP_DIR, True, True)
98-
self.assertEqual(self.get_invalid(io), bak)
95+
assert self.get_invalid(io) == bak
9996

10097
io.close()
10198

102-
@data('local', 'remote')
99+
@pytest.mark.parametrize('target', ['local', 'remote'])
103100
def test_backup(self, target):
104101
io = self.get_io(target)
105102

106103
bak = backup.Backup.new(io, context.BACKUP_DIR)
107104
status = bak.backup([context.TEST_DIR, context.TEST_FILE],
108105
excludes=['**/dir2/.gitkeep'])
109106

110-
self.assertEqual(status.exit_code, 0)
111-
self.assertTrue(os.path.exists(bak.backup_data_dir))
107+
assert status.exit_code == 0
108+
assert os.path.exists(bak.backup_data_dir)
112109

113110
# Define the structure we expect after the backup
114111
expected_data = test_utils.get_dir_struct(context.DATA_DIR)
@@ -119,19 +116,19 @@ def test_backup(self, target):
119116
context.DATA_DIR.lstrip('/'))
120117

121118
synced_data = test_utils.get_dir_struct(real_data_dir)
122-
self.assertEqual(expected_data, synced_data)
119+
assert expected_data == synced_data
123120

124121
io.close()
125122

126-
@data('local', 'remote')
123+
@pytest.mark.parametrize('target', ['local', 'remote'])
127124
def test_backup_pattern(self, target):
128125
io = self.get_io(target)
129126

130127
bak = backup.Backup.new(io, context.BACKUP_DIR)
131128
status = bak.backup([os.path.join(context.DATA_DIR, 'test*')],
132129
excludes=['**/dir2'])
133130

134-
self.assertEqual(status.exit_code, 0)
131+
assert status.exit_code == 0
135132

136133
# Define the structure we expect after the backup
137134
expected_data = test_utils.get_dir_struct(context.DATA_DIR)
@@ -141,81 +138,77 @@ def test_backup_pattern(self, target):
141138
context.DATA_DIR.lstrip('/'))
142139

143140
synced_data = test_utils.get_dir_struct(real_data_dir)
144-
self.assertEqual(expected_data, synced_data)
141+
assert expected_data == synced_data
145142

146143
io.close()
147144

148-
@data('local', 'remote')
145+
@pytest.mark.parametrize('target', ['local', 'remote'])
149146
def test_backup_dry_run(self, target):
150147
io = self.get_io(target)
151148

152149
bak = backup.Backup.new(io, context.BACKUP_DIR)
153150
status = bak.backup([context.TEST_DIR, context.TEST_FILE],
154151
dry_run=True)
155152

156-
self.assertEqual(status.exit_code, 0)
157-
self.assertTrue(not os.path.exists(bak.backup_dir))
153+
assert status.exit_code == 0
154+
assert not os.path.exists(bak.backup_dir)
158155

159156
io.close()
160157

161-
@data('local', 'remote')
158+
@pytest.mark.parametrize('target', ['local', 'remote'])
162159
def test_restore_items(self, target):
163160
io = self.get_io(target)
164161

165162
bak = self.get_valid(io)
166163
status = bak.restore(context.TMP_DIR, ['/tmp/test1.file'])
167164

168-
self.assertEqual(status.exit_code, 0)
165+
assert status.exit_code == 0
169166

170167
expected_data = test_utils.get_dir_struct(bak.backup_data_dir)
171168
del expected_data['tmp']['test2.file']
172169
del expected_data['tmp']['test3.file']
173170

174171
synced_data = test_utils.get_dir_struct(context.TMP_DIR)
175-
self.assertEqual(expected_data, synced_data)
172+
assert expected_data == synced_data
176173

177174
io.close()
178175

179-
@data('local', 'remote')
176+
@pytest.mark.parametrize('target', ['local', 'remote'])
180177
def test_restore_full(self, target):
181178
io = self.get_io(target)
182179

183180
bak = self.get_valid(io)
184181
status = bak.restore(context.TMP_DIR)
185182

186-
self.assertEqual(status.exit_code, 0)
183+
assert status.exit_code == 0
187184

188185
expected_data = test_utils.get_dir_struct(bak.backup_data_dir)
189186
synced_data = test_utils.get_dir_struct(context.TMP_DIR)
190-
self.assertEqual(expected_data, synced_data)
187+
assert expected_data == synced_data
191188

192189
io.close()
193190

194-
@data('local', 'remote')
191+
@pytest.mark.parametrize('target', ['local', 'remote'])
195192
def test_restore_dry_run(self, target):
196193
io = self.get_io(target)
197194

198195
bak = self.get_valid(io)
199196
status = bak.restore(context.TMP_DIR, dry_run=True)
200197

201-
self.assertEqual(status.exit_code, 0)
202-
self.assertTrue(not os.path.exists(context.TMP_DIR))
198+
assert status.exit_code == 0
199+
assert not os.path.exists(context.TMP_DIR)
203200

204201
io.close()
205202

206-
@data('local', 'remote')
203+
@pytest.mark.parametrize('target', ['local', 'remote'])
207204
def test_remove(self, target):
208205
io = self.get_io(target)
209206

210207
bak = backup.Backup.new(io, context.BACKUP_DIR)
211208
bak.backup([context.TEST_FILE])
212209

213-
self.assertTrue(os.path.exists(bak.backup_data_dir))
210+
assert os.path.exists(bak.backup_data_dir)
214211
bak.remove()
215-
self.assertTrue(not os.path.exists(bak.backup_data_dir))
212+
assert not os.path.exists(bak.backup_data_dir)
216213

217214
io.close()
218-
219-
220-
if __name__ == '__main__':
221-
unittest.main(exit=False)

0 commit comments

Comments
 (0)