Skip to content

Commit 1d81187

Browse files
committed
pytest: use assert (wip)
1 parent 97f29ea commit 1d81187

21 files changed

+196
-145
lines changed

test/test_blame.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@
2929

3030
from __future__ import absolute_import
3131
from __future__ import unicode_literals
32+
3233
import unittest
33-
import pygit2
34+
35+
import pytest
36+
3437
from pygit2 import Signature, Oid
35-
from pygit2 import GIT_DIFF_INCLUDE_UNMODIFIED
36-
from pygit2 import GIT_DIFF_IGNORE_WHITESPACE, GIT_DIFF_IGNORE_WHITESPACE_EOL
3738
from . import utils
38-
from itertools import chain
39-
from datetime import datetime
4039

4140
PATH = 'hello.txt'
4241

@@ -79,7 +78,7 @@ def test():
7978
blame[100000]
8079
blame[-1]
8180

82-
self.assertRaises(IndexError, test)
81+
with pytest.raises(IndexError): test()
8382

8483
def test_blame_for_line(self):
8584
repo = self.repo
@@ -107,7 +106,7 @@ def test():
107106
blame.for_line(100000)
108107
blame.for_line(-1)
109108

110-
self.assertRaises(IndexError, test)
109+
with pytest.raises(IndexError): test()
111110

112111
def test_blame_newest(self):
113112
repo = self.repo

test/test_blob.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import io
3434
import unittest
3535

36+
import pytest
37+
3638
import pygit2
3739
from . import utils
3840

@@ -113,7 +115,7 @@ def test_create_blob(self):
113115
def set_content():
114116
blob_buffer[:2] = b'hi'
115117

116-
self.assertRaises(TypeError, set_content)
118+
with pytest.raises(TypeError): set_content()
117119

118120
def test_create_blob_fromworkdir(self):
119121

@@ -132,8 +134,8 @@ def test_create_blob_fromworkdir(self):
132134

133135

134136
def test_create_blob_outside_workdir(self):
135-
path = __file__
136-
self.assertRaises(KeyError, self.repo.create_blob_fromworkdir, path)
137+
with pytest.raises(KeyError):
138+
self.repo.create_blob_fromworkdir(__file__)
137139

138140

139141
def test_create_blob_fromdisk(self):
@@ -144,7 +146,8 @@ def test_create_blob_fromdisk(self):
144146
assert pygit2.GIT_OBJ_BLOB == blob.type
145147

146148
def test_create_blob_fromiobase(self):
147-
self.assertRaises(TypeError, self.repo.create_blob_fromiobase, 'bad type')
149+
with pytest.raises(TypeError):
150+
self.repo.create_blob_fromiobase('bad type')
148151

149152
f = io.BytesIO(BLOB_CONTENT)
150153
blob_oid = self.repo.create_blob_fromiobase(f)

test/test_branch.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929

3030
from __future__ import absolute_import
3131
from __future__ import unicode_literals
32+
3233
import unittest
3334

35+
import pytest
36+
3437
import pygit2
3538
from . import utils
3639

@@ -51,7 +54,7 @@ def test_lookup_branch_local(self):
5154

5255
assert self.repo.branches.get('not-exists') is None
5356

54-
self.assertRaises(KeyError, lambda: self.repo.branches['not-exists'])
57+
with pytest.raises(KeyError): self.repo.branches['not-exists']
5558

5659
def test_listall_branches(self):
5760
branches = sorted(self.repo.branches)
@@ -65,8 +68,8 @@ def test_create_branch(self):
6568
assert reference.target.hex == LAST_COMMIT
6669

6770
# try to create existing reference
68-
self.assertRaises(ValueError,
69-
lambda: self.repo.branches.create('version1', commit))
71+
with pytest.raises(ValueError):
72+
self.repo.branches.create('version1', commit)
7073

7174
# try to create existing reference with force
7275
reference = self.repo.branches.create('version1', commit, True)
@@ -78,7 +81,8 @@ def test_delete(self):
7881
assert self.repo.branches.get('i18n') is None
7982

8083
def test_cant_delete_master(self):
81-
self.assertRaises(pygit2.GitError, lambda: self.repo.branches.delete('master'))
84+
with pytest.raises(pygit2.GitError):
85+
self.repo.branches.delete('master')
8286

8387
def test_branch_is_head_returns_true_if_branch_is_head(self):
8488
branch = self.repo.branches.get('master')
@@ -97,7 +101,7 @@ def test_branch_rename_succeeds(self):
97101

98102
def test_branch_rename_fails_if_destination_already_exists(self):
99103
original_branch = self.repo.branches.get('i18n')
100-
self.assertRaises(ValueError, lambda: original_branch.rename('master'))
104+
with pytest.raises(ValueError): original_branch.rename('master')
101105

102106
def test_branch_rename_not_fails_if_force_is_true(self):
103107
original_branch = self.repo.branches.get('master')
@@ -106,8 +110,8 @@ def test_branch_rename_not_fails_if_force_is_true(self):
106110

107111
def test_branch_rename_fails_with_invalid_names(self):
108112
original_branch = self.repo.branches.get('i18n')
109-
self.assertRaises(ValueError,
110-
lambda: original_branch.rename('abc@{123'))
113+
with pytest.raises(ValueError):
114+
original_branch.rename('abc@{123')
111115

112116
def test_branch_name(self):
113117
branch = self.repo.branches.get('master')
@@ -172,7 +176,7 @@ def test_branch_upstream(self):
172176
def set_bad_upstream():
173177
master.upstream = 2.5
174178

175-
self.assertRaises(TypeError, set_bad_upstream)
179+
with pytest.raises(TypeError): set_bad_upstream()
176180

177181
master.upstream = None
178182
assert master.upstream is None
@@ -210,8 +214,8 @@ def test_create_branch(self):
210214
assert reference.target.hex == LAST_COMMIT
211215

212216
# try to create existing reference
213-
self.assertRaises(ValueError,
214-
lambda: self.repo.create_branch('version1', commit))
217+
with pytest.raises(ValueError):
218+
self.repo.create_branch('version1', commit)
215219

216220
# try to create existing reference with force
217221
reference = self.repo.create_branch('version1', commit, True)
@@ -226,7 +230,7 @@ def test_delete(self):
226230
def test_cant_delete_master(self):
227231
branch = self.repo.lookup_branch('master')
228232

229-
self.assertRaises(pygit2.GitError, lambda: branch.delete())
233+
with pytest.raises(pygit2.GitError): branch.delete()
230234

231235
def test_branch_is_head_returns_true_if_branch_is_head(self):
232236
branch = self.repo.lookup_branch('master')
@@ -254,7 +258,7 @@ def test_branch_rename_succeeds(self):
254258

255259
def test_branch_rename_fails_if_destination_already_exists(self):
256260
original_branch = self.repo.lookup_branch('i18n')
257-
self.assertRaises(ValueError, lambda: original_branch.rename('master'))
261+
with pytest.raises(ValueError): original_branch.rename('master')
258262

259263
def test_branch_rename_not_fails_if_force_is_true(self):
260264
original_branch = self.repo.lookup_branch('master')
@@ -263,8 +267,8 @@ def test_branch_rename_not_fails_if_force_is_true(self):
263267

264268
def test_branch_rename_fails_with_invalid_names(self):
265269
original_branch = self.repo.lookup_branch('i18n')
266-
self.assertRaises(ValueError,
267-
lambda: original_branch.rename('abc@{123'))
270+
with pytest.raises(ValueError):
271+
original_branch.rename('abc@{123')
268272

269273
def test_branch_name(self):
270274
branch = self.repo.lookup_branch('master')
@@ -314,7 +318,7 @@ def test_branch_upstream(self):
314318
def set_bad_upstream():
315319
master.upstream = 2.5
316320

317-
self.assertRaises(TypeError, set_bad_upstream)
321+
with pytest.raises(TypeError): set_bad_upstream()
318322

319323
master.upstream = None
320324
assert master.upstream is None

test/test_cherrypick.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,29 @@
3131
from __future__ import absolute_import
3232
from __future__ import unicode_literals
3333

34-
import unittest
3534
import os
3635

37-
from pygit2 import GIT_MERGE_ANALYSIS_NONE, GIT_MERGE_ANALYSIS_NORMAL, GIT_MERGE_ANALYSIS_UP_TO_DATE
38-
from pygit2 import GIT_MERGE_ANALYSIS_FASTFORWARD, GIT_MERGE_ANALYSIS_UNBORN
39-
import pygit2
36+
import pytest
4037

38+
import pygit2
4139
from . import utils
4240

4341
class CherrypickTestBasic(utils.RepoTestCaseForMerging):
4442

4543
def test_cherrypick_none(self):
46-
self.assertRaises(TypeError, self.repo.cherrypick, None)
44+
with pytest.raises(TypeError): self.repo.cherrypick(None)
4745

4846
def test_cherrypick_invalid_hex(self):
4947
branch_head_hex = '12345678'
50-
self.assertRaises(KeyError, self.repo.cherrypick, branch_head_hex)
48+
with pytest.raises(KeyError): self.repo.cherrypick(branch_head_hex)
5149

5250
def test_cherrypick_already_something_in_index(self):
5351
branch_head_hex = '03490f16b15a09913edb3a067a3dc67fbb8d41f1'
5452
branch_oid = self.repo.get(branch_head_hex).id
5553
with open(os.path.join(self.repo.workdir, 'inindex.txt'), 'w') as f:
5654
f.write('new content')
5755
self.repo.index.add('inindex.txt')
58-
self.assertRaises(pygit2.GitError, self.repo.cherrypick, branch_oid)
56+
with pytest.raises(pygit2.GitError): self.repo.cherrypick(branch_oid)
5957

6058
class CherrypickTestWithConflicts(utils.RepoTestCaseForMerging):
6159

@@ -67,5 +65,5 @@ def test_cherrypick_remove_conflicts(self):
6765
assert conflicts is not None
6866
conflicts['.gitignore']
6967
del idx.conflicts['.gitignore']
70-
self.assertRaises(KeyError, conflicts.__getitem__, '.gitignore')
68+
with pytest.raises(KeyError): conflicts.__getitem__('.gitignore')
7169
assert idx.conflicts is None

test/test_commit.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import unittest
3434
import sys
3535

36+
import pytest
37+
3638
from pygit2 import GIT_OBJ_COMMIT, Signature, Oid
3739
from . import utils
3840

@@ -95,8 +97,8 @@ def test_new_commit(self):
9597
too_short_prefix = tree[:3]
9698

9799
parents = [COMMIT_SHA[:5]]
98-
self.assertRaises(ValueError, repo.create_commit, None, author,
99-
committer, message, too_short_prefix, parents)
100+
with pytest.raises(ValueError):
101+
repo.create_commit(None, author, committer, message, too_short_prefix, parents)
100102

101103
sha = repo.create_commit(None, author, committer, message,
102104
tree_prefix, parents)
@@ -152,11 +154,11 @@ def test_modify_commit(self):
152154
commit = self.repo[COMMIT_SHA]
153155

154156
error_type = AttributeError if not pypy2 else TypeError
155-
self.assertRaises(error_type, setattr, commit, 'message', message)
156-
self.assertRaises(error_type, setattr, commit, 'committer', committer)
157-
self.assertRaises(error_type, setattr, commit, 'author', author)
158-
self.assertRaises(error_type, setattr, commit, 'tree', None)
159-
self.assertRaises(error_type, setattr, commit, 'parents', None)
157+
with pytest.raises(error_type): setattr(commit, 'message', message)
158+
with pytest.raises(error_type): setattr(commit, 'committer', committer)
159+
with pytest.raises(error_type): setattr(commit, 'author', author)
160+
with pytest.raises(error_type): setattr(commit, 'tree', None)
161+
with pytest.raises(error_type): setattr(commit, 'parents', None)
160162

161163

162164
class GpgSignatureTestCase(utils.GpgSignedRepoTestCase):

test/test_config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import os
3131
import unittest
3232

33+
import pytest
34+
3335
from pygit2 import Config
3436
from . import utils
3537

@@ -95,8 +97,8 @@ def test_add(self):
9597
def test_read(self):
9698
config = self.repo.config
9799

98-
self.assertRaises(TypeError, lambda: config[()])
99-
self.assertRaises(TypeError, lambda: config[-4])
100+
with pytest.raises(TypeError): config[()]
101+
with pytest.raises(TypeError): config[-4]
100102
self.assertRaisesWithArg(ValueError, "invalid config item name 'abc'",
101103
lambda: config['abc'])
102104
self.assertRaisesWithArg(KeyError, 'abc.def',
@@ -124,8 +126,8 @@ def test_read(self):
124126
def test_write(self):
125127
config = self.repo.config
126128

127-
self.assertRaises(TypeError, config.__setitem__,
128-
(), 'This should not work')
129+
with pytest.raises(TypeError):
130+
config.__setitem__((), 'This should not work')
129131

130132
assert 'core.dummy1' not in config
131133
config['core.dummy1'] = 42

test/test_credentials.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030

3131
import unittest
32+
33+
import pytest
34+
3235
import pygit2
3336
from pygit2 import GIT_CREDTYPE_USERPASS_PLAINTEXT
3437
from pygit2 import Username, UserPass, Keypair, KeypairFromAgent, KeypairFromMemory
@@ -93,7 +96,7 @@ def credentials(url, username, allowed):
9396
url = "https://github.com/github/github"
9497
remote = self.repo.create_remote("github", url)
9598

96-
self.assertRaises(Exception, lambda: remote.fetch(callbacks=MyCallbacks()))
99+
with pytest.raises(Exception): remote.fetch(callbacks=MyCallbacks())
97100

98101
@unittest.skipIf(utils.no_network(), "Requires network")
99102
def test_bad_cred_type(self):
@@ -105,7 +108,7 @@ def credentials(url, username, allowed):
105108

106109
url = "https://github.com/github/github"
107110
remote = self.repo.create_remote("github", url)
108-
self.assertRaises(TypeError, lambda: remote.fetch(callbacks=MyCallbacks()))
111+
with pytest.raises(TypeError): remote.fetch(callbacks=MyCallbacks())
109112

110113
class CallableCredentialTest(utils.RepoTestCase):
111114

test/test_describe.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929

3030
from __future__ import absolute_import
3131
from __future__ import unicode_literals
32-
import unittest
3332

34-
from pygit2 import GIT_DESCRIBE_DEFAULT, GIT_DESCRIBE_TAGS, GIT_DESCRIBE_ALL
33+
import pytest
34+
35+
from pygit2 import GIT_DESCRIBE_TAGS, GIT_DESCRIBE_ALL
3536
import pygit2
3637
from . import utils
3738

@@ -51,7 +52,7 @@ def test_describe(self):
5152
assert 'thetag-2-g2be5719' == self.repo.describe()
5253

5354
def test_describe_without_ref(self):
54-
self.assertRaises(pygit2.GitError, self.repo.describe)
55+
with pytest.raises(pygit2.GitError): self.repo.describe()
5556

5657
def test_describe_default_oid(self):
5758
assert '2be5719' == self.repo.describe(show_commit_oid_as_fallback=True)
@@ -60,7 +61,7 @@ def test_describe_strategies(self):
6061
assert 'heads/master' == self.repo.describe(describe_strategy=GIT_DESCRIBE_ALL)
6162

6263
self.repo.create_reference('refs/tags/thetag', '4ec4389a8068641da2d6578db0419484972284c8')
63-
self.assertRaises(KeyError, self.repo.describe)
64+
with pytest.raises(KeyError): self.repo.describe()
6465
assert 'thetag-2-g2be5719' == self.repo.describe(describe_strategy=GIT_DESCRIBE_TAGS)
6566

6667
def test_describe_pattern(self):
@@ -81,7 +82,8 @@ def test_describe_committish(self):
8182

8283
def test_describe_follows_first_branch_only(self):
8384
add_tag(self.repo, 'thetag', '4ec4389a8068641da2d6578db0419484972284c8')
84-
self.assertRaises(KeyError, self.repo.describe, only_follow_first_parent=True)
85+
with pytest.raises(KeyError):
86+
self.repo.describe(only_follow_first_parent=True)
8587

8688
def test_describe_abbreviated_size(self):
8789
add_tag(self.repo, 'thetag', '4ec4389a8068641da2d6578db0419484972284c8')

0 commit comments

Comments
 (0)