Skip to content

Commit 1e3c419

Browse files
kurtmckeeLudovicRousseau
authored andcommitted
Migrate to pytest features
This PR introduces the following changes: - Migrate the test suite to use pytest features - Address a duplicated test name in test_Exceptions.py: test_ListReadersException. The duplicate tests have been merged. Most of this work was mechanical: - Remove unittest.TestCase subclass definition lines - De-dent the class methods - Remove references to self in the function parameters - Use regular expressions to search and replace assertions: - self.assertEqual(x, y) -> assert x == y - self.assertRaises -> pytest.raises - self.assertFalse(x) -> assert x is False - self.assertIn(x, y) -> assert x in y Some long strings were wrapped to prepare for the possibility of using code formatters in the future. STDOUT capture code was replaced with the pytest capsys fixture, which simplified the code in test_ATR.py. This PR prepares for the possibility of consolidating the live tests in src/smartcard/test/ together with the overall test suite, and using pytest features to control whether the live tests run or not. This is only a possibility, not a guarantee, but migrating from unittest code to pytest code helps pave the way.
1 parent 51c268a commit 1e3c419

File tree

7 files changed

+564
-528
lines changed

7 files changed

+564
-528
lines changed

test/__init__.py

Whitespace-only changes.

test/test_ATR.py

Lines changed: 102 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,97 @@
1-
# -*- coding: utf-8 -*-
1+
import pytest
22

3-
import io
4-
import sys
5-
import unittest
63
from smartcard.ATR import ATR
74
from smartcard.Exceptions import SmartcardException
85
from smartcard.util import toBytes
96

107

11-
class TestUtil(unittest.TestCase):
12-
13-
def setUp(self):
14-
self.held, sys.stdout = sys.stdout, io.StringIO()
15-
16-
def test_ATR1(self):
17-
atr = [0x3F, 0x65, 0x25, 0x00, 0x2C, 0x09, 0x69, 0x90, 0x00]
18-
data_out = """TB1: 25
8+
def test_atr1(capsys):
9+
atr = [0x3F, 0x65, 0x25, 0x00, 0x2C, 0x09, 0x69, 0x90, 0x00]
10+
data_out = """TB1: 25
1911
TC1: 0
2012
supported protocols T=0
2113
T=0 supported: True
2214
T=1 supported: False
23-
clock rate conversion factor: 372
24-
bit rate adjustment factor: 1
25-
maximum programming current: 50
26-
programming voltage: 30
27-
guard time: 0
15+
\tclock rate conversion factor: 372
16+
\tbit rate adjustment factor: 1
17+
\tmaximum programming current: 50
18+
\tprogramming voltage: 30
19+
\tguard time: 0
2820
nb of interface bytes: 2
2921
nb of historical bytes: 5
3022
"""
31-
a = ATR(atr)
32-
a.dump()
33-
output = sys.stdout.getvalue()
34-
self.assertEqual(output, data_out)
35-
36-
def test_ATR2(self):
37-
atr = [0x3F, 0x65, 0x25, 0x08, 0x93, 0x04, 0x6C, 0x90, 0x00]
38-
data_out = """TB1: 25
23+
a = ATR(atr)
24+
a.dump()
25+
stdout, _ = capsys.readouterr()
26+
assert stdout == data_out
27+
28+
29+
def test_atr2(capsys):
30+
atr = [0x3F, 0x65, 0x25, 0x08, 0x93, 0x04, 0x6C, 0x90, 0x00]
31+
data_out = """TB1: 25
3932
TC1: 8
4033
supported protocols T=0
4134
T=0 supported: True
4235
T=1 supported: False
43-
clock rate conversion factor: 372
44-
bit rate adjustment factor: 1
45-
maximum programming current: 50
46-
programming voltage: 30
47-
guard time: 8
36+
\tclock rate conversion factor: 372
37+
\tbit rate adjustment factor: 1
38+
\tmaximum programming current: 50
39+
\tprogramming voltage: 30
40+
\tguard time: 8
4841
nb of interface bytes: 2
4942
nb of historical bytes: 5
5043
"""
51-
a = ATR(atr)
52-
a.dump()
53-
output = sys.stdout.getvalue()
54-
self.assertEqual(output, data_out)
55-
56-
def test_ATR3(self):
57-
atr = [0x3B, 0x16, 0x94, 0x7C, 0x03, 0x01, 0x00, 0x00, 0x0D]
58-
data_out = """TA1: 94
44+
a = ATR(atr)
45+
a.dump()
46+
47+
stdout, _ = capsys.readouterr()
48+
assert stdout == data_out
49+
50+
51+
def test_atr3(capsys):
52+
atr = [0x3B, 0x16, 0x94, 0x7C, 0x03, 0x01, 0x00, 0x00, 0x0D]
53+
data_out = """TA1: 94
5954
supported protocols T=0
6055
T=0 supported: True
6156
T=1 supported: False
62-
clock rate conversion factor: 512
63-
bit rate adjustment factor: 8
64-
maximum programming current: 50
65-
programming voltage: 5
66-
guard time: None
57+
\tclock rate conversion factor: 512
58+
\tbit rate adjustment factor: 8
59+
\tmaximum programming current: 50
60+
\tprogramming voltage: 5
61+
\tguard time: None
6762
nb of interface bytes: 1
6863
nb of historical bytes: 6
6964
"""
70-
a = ATR(atr)
71-
a.dump()
72-
output = sys.stdout.getvalue()
73-
self.assertEqual(output, data_out)
74-
75-
def test_ATR4(self):
76-
atr = [0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03]
77-
data_out = """TB1: 0
65+
a = ATR(atr)
66+
a.dump()
67+
stdout, _ = capsys.readouterr()
68+
assert stdout == data_out
69+
70+
71+
def test_atr4(capsys):
72+
atr = [0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03]
73+
data_out = """TB1: 0
7874
TC1: 0
7975
supported protocols T=0
8076
T=0 supported: True
8177
T=1 supported: False
82-
clock rate conversion factor: 372
83-
bit rate adjustment factor: 1
84-
maximum programming current: 25
85-
programming voltage: 5
86-
guard time: 0
78+
\tclock rate conversion factor: 372
79+
\tbit rate adjustment factor: 1
80+
\tmaximum programming current: 25
81+
\tprogramming voltage: 5
82+
\tguard time: 0
8783
nb of interface bytes: 2
8884
nb of historical bytes: 5
8985
"""
90-
a = ATR(atr)
91-
a.dump()
92-
output = sys.stdout.getvalue()
93-
self.assertEqual(output, data_out)
94-
95-
def test_ATR5(self):
96-
atr = [0x3B, 0xE3, 0x00, 0xFF, 0x81, 0x31, 0x52, 0x45, 0xA1,
97-
0xA2, 0xA3, 0x1B]
98-
data_out = """TB1: 0
86+
a = ATR(atr)
87+
a.dump()
88+
stdout, _ = capsys.readouterr()
89+
assert stdout == data_out
90+
91+
92+
def test_atr5(capsys):
93+
atr = [0x3B, 0xE3, 0x00, 0xFF, 0x81, 0x31, 0x52, 0x45, 0xA1, 0xA2, 0xA3, 0x1B]
94+
data_out = """TB1: 0
9995
TC1: ff
10096
TD1: 81
10197
TD2: 31
@@ -105,23 +101,23 @@ def test_ATR5(self):
105101
T=0 supported: False
106102
T=1 supported: True
107103
checksum: 27
108-
clock rate conversion factor: 372
109-
bit rate adjustment factor: 1
110-
maximum programming current: 25
111-
programming voltage: 5
112-
guard time: 255
104+
\tclock rate conversion factor: 372
105+
\tbit rate adjustment factor: 1
106+
\tmaximum programming current: 25
107+
\tprogramming voltage: 5
108+
\tguard time: 255
113109
nb of interface bytes: 6
114110
nb of historical bytes: 3
115111
"""
116-
a = ATR(atr)
117-
a.dump()
118-
output = sys.stdout.getvalue()
119-
self.assertEqual(output, data_out)
120-
121-
def test_ATR6(self):
122-
atr = [0x3B, 0xE5, 0x00, 0x00, 0x81, 0x21, 0x45, 0x9C, 0x10,
123-
0x01, 0x00, 0x80, 0x0D]
124-
data_out = """TB1: 0
112+
a = ATR(atr)
113+
a.dump()
114+
stdout, _ = capsys.readouterr()
115+
assert stdout == data_out
116+
117+
118+
def test_atr6(capsys):
119+
atr = [0x3B, 0xE5, 0x00, 0x00, 0x81, 0x21, 0x45, 0x9C, 0x10, 0x01, 0x00, 0x80, 0x0D]
120+
data_out = """TB1: 0
125121
TC1: 0
126122
TD1: 81
127123
TD2: 21
@@ -130,34 +126,33 @@ def test_ATR6(self):
130126
T=0 supported: False
131127
T=1 supported: True
132128
checksum: 13
133-
clock rate conversion factor: 372
134-
bit rate adjustment factor: 1
135-
maximum programming current: 25
136-
programming voltage: 5
137-
guard time: 0
129+
\tclock rate conversion factor: 372
130+
\tbit rate adjustment factor: 1
131+
\tmaximum programming current: 25
132+
\tprogramming voltage: 5
133+
\tguard time: 0
138134
nb of interface bytes: 5
139135
nb of historical bytes: 5
140136
"""
141-
a = ATR(atr)
142-
a.dump()
143-
output = sys.stdout.getvalue()
144-
self.assertEqual(output, data_out)
145-
146-
def test_ATR_TS(self):
147-
atr = [0x42]
148-
with self.assertRaises(SmartcardException):
149-
ATR(atr)
150-
151-
def test_ATR_get(self):
152-
atr = "3B F2 95 12 34 01 36 06"
153-
a = ATR(toBytes(atr))
154-
self.assertEqual(a.getTA1(), 0x95)
155-
self.assertEqual(a.getTB1(), 0x12)
156-
self.assertEqual(a.getTC1(), 0x34)
157-
self.assertEqual(a.getTD1(), 0x01)
158-
self.assertEqual(a.getHistoricalBytes(), [0x36, 0x06])
159-
self.assertFalse(a.isT15Supported())
160-
self.assertEqual(str(a), atr)
161-
162-
if __name__ == '__main__':
163-
unittest.main(buffer=True)
137+
a = ATR(atr)
138+
a.dump()
139+
stdout, _ = capsys.readouterr()
140+
assert stdout == data_out
141+
142+
143+
def test_atr_ts():
144+
atr = [0x42]
145+
with pytest.raises(SmartcardException):
146+
ATR(atr)
147+
148+
149+
def test_atr_get():
150+
atr = "3B F2 95 12 34 01 36 06"
151+
a = ATR(toBytes(atr))
152+
assert a.getTA1() == 0x95
153+
assert a.getTB1() == 0x12
154+
assert a.getTC1() == 0x34
155+
assert a.getTD1() == 0x01
156+
assert a.getHistoricalBytes(), [0x36 == 0x06]
157+
assert a.isT15Supported() is False
158+
assert str(a) == atr

0 commit comments

Comments
 (0)