Skip to content

Commit 2d1923f

Browse files
committed
refactor(tests): finish converting all tests to pytest
1 parent 77c3e58 commit 2d1923f

8 files changed

Lines changed: 127 additions & 157 deletions

g2p/tests/test_neural.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#!/usr/bin/env python
22

33
import sys
4-
from unittest import SkipTest, TestCase
54

6-
from pytest import main
5+
from pytest import main, skip
76

87
from g2p import make_g2p
98
from g2p.log import LOGGER
109
from g2p.mappings.utils import has_neural_support
1110
from g2p.tests.public.data import load_neural_test_data
1211

1312

14-
class NeuralLangTest(TestCase):
13+
class TestNeuralLang:
1514
"""Basic Test for individual lookup tables.
1615
1716
Test file is in g2p/tests/public/data/neural.psv.
@@ -21,10 +20,8 @@ class NeuralLangTest(TestCase):
2120
"""
2221

2322
def test_io(self):
24-
if sys.version_info < (3, 9):
25-
raise SkipTest("neural g2p requires python >= 3.9")
2623
if not has_neural_support():
27-
raise SkipTest("neural not installed; skipping neural tests")
24+
skip("neural not installed; skipping neural tests")
2825
langs_to_test = load_neural_test_data()
2926

3027
# go through each language declared in the test case set up

g2p/tests/test_studio.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import time
1717
from datetime import datetime
1818
from random import sample
19-
from unittest import IsolatedAsyncioTestCase
2019

2120
import socketio # type: ignore
2221
from playwright.async_api import Error, async_playwright, expect # type: ignore
23-
from pytest import fixture, main
22+
from pytest import fixture, main, mark
2423

2524
from g2p.log import LOGGER
2625
from g2p.tests.public.data import load_public_test_data
@@ -30,9 +29,7 @@
3029

3130
@fixture(autouse=True, scope="module")
3231
def run_studio():
33-
"""Launch the studio server automatically via this fuxture (pytest only)
34-
35-
When using unittest, launch run_studio.py in another window first."""
32+
"""Launch the studio server automatically via this fuxture"""
3633
import threading
3734

3835
def start_studio():
@@ -56,13 +53,12 @@ def start_studio():
5653
yield
5754

5855

59-
class StudioTest(IsolatedAsyncioTestCase):
60-
def __init__(self, *args):
61-
super().__init__(*args)
62-
# self.port = 5000
63-
self.port = STUDIO_PORT
64-
self.debug_convert = True
65-
self.timeout_delay = 500
56+
@mark.asyncio
57+
class TestStudio:
58+
# self.port = 5000
59+
port = STUDIO_PORT
60+
debug_convert = True
61+
timeout_delay = 500
6662

6763
async def test_socket_connection(self):
6864
client = socketio.AsyncClient()

g2p/tests/test_tokenize_and_map.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
#!/usr/bin/env python
22

33
import sys
4-
from unittest import TestCase
54

6-
from pytest import main
5+
from pytest import main, raises
76

87
import g2p
98

109

11-
class TokenizeAndMapTest(TestCase):
10+
class TestTokenizeAndMap:
1211
"""Test suite for chaining tokenization and transduction"""
1312

14-
def setUp(self):
15-
pass
16-
1713
def contextualize(self, word: str):
1814
return word + " " + word + " ," + word + ", " + word
1915

@@ -162,37 +158,32 @@ def test_removed_tok_langs_in_v2(self) -> None:
162158
# monkey patch to make sure we always exercise the TypeError pathway
163159
saved_version = g2p._version.VERSION
164160
g2p._version.VERSION = "2.0"
165-
with self.assertRaises(TypeError):
161+
with raises(TypeError):
166162
_ = g2p.make_g2p("iku-sro", "eng-ipa", "path") # type: ignore
167163
g2p._version.VERSION = saved_version
168164

169165
def test_make_g2p_cache(self):
170-
self.assertIs(
171-
g2p.make_g2p("fra", "fra-ipa", tokenize=False),
172-
g2p.make_g2p("fra", "fra-ipa", tokenize=False),
166+
assert g2p.make_g2p("fra", "fra-ipa", tokenize=False) is g2p.make_g2p(
167+
"fra", "fra-ipa", tokenize=False
173168
)
174-
175-
self.assertIsNot(
176-
g2p.make_g2p("fra", "fra-ipa", tokenize=True),
177-
g2p.make_g2p("fra", "fra-ipa", tokenize=False),
169+
assert g2p.make_g2p("fra", "fra-ipa", tokenize=True) is not g2p.make_g2p(
170+
"fra", "fra-ipa", tokenize=False
178171
)
179172
my_tokenizer = g2p.make_tokenizer("oji")
180-
self.assertIs(
181-
g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=my_tokenizer),
182-
g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=my_tokenizer),
183-
)
184-
self.assertIs(
185-
g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=my_tokenizer),
186-
g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=g2p.make_tokenizer("oji")),
187-
)
188-
self.assertIsNot(
189-
g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=my_tokenizer),
190-
g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=g2p.make_tokenizer("ikt")),
191-
)
192-
self.assertIsNot(
193-
g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=my_tokenizer),
194-
g2p.make_g2p("oji", "eng-ipa", tokenize=True),
173+
assert g2p.make_g2p(
174+
"oji", "eng-ipa", custom_tokenizer=my_tokenizer
175+
) is g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=my_tokenizer)
176+
assert g2p.make_g2p(
177+
"oji", "eng-ipa", custom_tokenizer=my_tokenizer
178+
) is g2p.make_g2p("oji", "eng-ipa", custom_tokenizer=g2p.make_tokenizer("oji"))
179+
assert g2p.make_g2p(
180+
"oji", "eng-ipa", custom_tokenizer=my_tokenizer
181+
) is not g2p.make_g2p(
182+
"oji", "eng-ipa", custom_tokenizer=g2p.make_tokenizer("ikt")
195183
)
184+
assert g2p.make_g2p(
185+
"oji", "eng-ipa", custom_tokenizer=my_tokenizer
186+
) is not g2p.make_g2p("oji", "eng-ipa", tokenize=True)
196187

197188

198189
if __name__ == "__main__":

g2p/tests/test_tokenizer.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#!/usr/bin/env python
22

33
import sys
4-
from unittest import TestCase
54

6-
from pytest import main
5+
from pytest import main, raises
76

87
import g2p.mappings.tokenizer as tok
98
from g2p.log import LOGGER
109

1110

12-
class TokenizerTest(TestCase):
11+
class TestTokenizer:
1312
"""Test suite for tokenizing text in a language-specific way"""
1413

1514
def test_tokenize_fra(self):
@@ -19,19 +18,19 @@ def test_tokenize_fra(self):
1918
assert len(tokens) == 8
2019
assert tokens[0].is_word
2120
assert tokens[0].text == "ceci"
22-
self.assertFalse(tokens[1].is_word)
21+
assert not tokens[1].is_word
2322
assert tokens[1].text == " "
2423
assert tokens[2].is_word
2524
assert tokens[2].text == "était"
26-
self.assertFalse(tokens[3].is_word)
25+
assert not tokens[3].is_word
2726
assert tokens[3].text == " '"
2827
assert tokens[4].is_word
2928
assert tokens[4].text == "un"
30-
self.assertFalse(tokens[5].is_word)
29+
assert not tokens[5].is_word
3130
assert tokens[5].text == "' "
3231
assert tokens[6].is_word
3332
assert tokens[6].text == "test"
34-
self.assertFalse(tokens[7].is_word)
33+
assert not tokens[7].is_word
3534
assert tokens[7].text == "."
3635

3736
def test_tokenize_eng(self):
@@ -41,10 +40,10 @@ def test_tokenize_eng(self):
4140
assert len(tokens) == 8
4241
assert tokens[0].is_word
4342
assert tokens[0].text == "This"
44-
self.assertFalse(tokens[1].is_word)
43+
assert not tokens[1].is_word
4544
assert tokens[1].text == " "
4645

47-
def test_lexicon_tokenizer(self):
46+
def test_lexicon_tokenizer(self, subtests):
4847
tokenizer = tok.make_tokenizer("eng")
4948
tests = [
5049
("It's", ["It's"]),
@@ -58,7 +57,7 @@ def test_lexicon_tokenizer(self):
5857
("all-in: nonsense", ["all", "-", "in", ": ", "nonsense"]), # all-in is not
5958
]
6059
for input_text, expected_tokens in tests:
61-
with self.subTest(input_text=input_text):
60+
with subtests.test(input_text=input_text):
6261
tokens = tokenizer.tokenize_text(input_text)
6362
assert [x.text for x in tokens] == expected_tokens
6463

@@ -100,10 +99,10 @@ def test_tokenize_tce_equiv(self):
10099
assert len(tok.make_tokenizer("tce").tokenize_text(input)) == 7
101100

102101
def test_tokenizer_identity_tce(self):
103-
self.assertNotEqual(tok.make_tokenizer("eng"), tok.make_tokenizer("fra"))
104-
self.assertNotEqual(tok.make_tokenizer("eng"), tok.make_tokenizer("tce"))
102+
assert tok.make_tokenizer("eng") != tok.make_tokenizer("fra")
103+
assert tok.make_tokenizer("eng") != tok.make_tokenizer("tce")
105104
assert tok.make_tokenizer("tce") == tok.make_tokenizer("tce")
106-
self.assertNotEqual(tok.make_tokenizer("tce"), tok.make_tokenizer())
105+
assert tok.make_tokenizer("tce") != tok.make_tokenizer()
107106
assert tok.make_tokenizer("foo") == tok.make_tokenizer()
108107

109108
def test_tokenize_kwk(self):
@@ -123,25 +122,25 @@ def test_three_hop_tokenizer(self):
123122

124123
def test_tokenize_not_ipa_explicit(self):
125124
tokenizer = tok.make_tokenizer("fn-unicode-font", "fn-unicode")
126-
self.assertNotEqual(tokenizer, tok.make_tokenizer())
125+
assert tokenizer != tok.make_tokenizer()
127126

128127
def test_tokenize_not_ipa_implicit(self):
129128
tokenizer = tok.make_tokenizer("fn-unicode-font")
130-
self.assertNotEqual(tokenizer, tok.make_tokenizer())
129+
assert tokenizer != tok.make_tokenizer()
131130

132131
def test_tokenize_lang_does_not_exist(self):
133132
assert tok.make_tokenizer("not_a_language") == tok.make_tokenizer()
134133
assert tok.make_tokenizer("fra" == "not_a_language"), tok.make_tokenizer()
135134

136135
def test_make_tokenizer_error(self):
137-
with self.assertRaises(ValueError):
136+
with raises(ValueError):
138137
_ = tok.make_tokenizer("fra", "eng-arpabet", ["fra-ipa", "eng-ipa"])
139138

140-
def test_deprecated_warning(self):
141-
with self.assertLogs(LOGGER, level="WARNING") as cm:
139+
def test_deprecated_warning(self, caplog):
140+
with caplog.at_level("WARNING", logger=LOGGER.name):
142141
tok._deprecated_warning_printed = False
143142
assert tok.get_tokenizer("fra") == tok.make_tokenizer("fra")
144-
assert "deprecated" in "".join(cm.output)
143+
assert "deprecated" in caplog.text
145144

146145
def test_gwi_multichar_grapheme_makeg2p(self):
147146
from g2p import make_g2p

g2p/tests/test_transducer.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import os
44
import sys
5-
from unittest import TestCase, mock
5+
from unittest import mock
66

7-
from pytest import main
7+
from pytest import main, raises
88

99
from g2p import make_g2p
1010
from g2p.exceptions import MalformedMapping, NeuralDependencyError
@@ -16,11 +16,11 @@
1616
# mypy: disable-error-code="attr-defined"
1717

1818

19-
class TransducerTest(TestCase):
19+
class TestTransducer:
2020
"""Basic Transducer Test"""
2121

2222
@classmethod
23-
def setUpClass(cls):
23+
def setup_class(cls):
2424
cls.test_mapping_moh = Mapping.find_mapping(
2525
in_lang="moh-equiv", out_lang="moh-ipa"
2626
)
@@ -93,9 +93,9 @@ def setUpClass(cls):
9393
def test_no_neural_dependencies(self):
9494
"""This tests what happens if a user tries to create a neural g2p without installing the dependencies. Other neural tests (for when deps are installed are in test_neural.py module.)"""
9595
with mock.patch("g2p.mappings.utils.has_neural_support", return_value=False):
96-
with self.assertRaises(NeuralDependencyError):
96+
with raises(NeuralDependencyError):
9797
make_g2p("foo", "bar", neural=True)
98-
with self.assertRaises(NeuralDependencyError):
98+
with raises(NeuralDependencyError):
9999
make_g2p("str", "str-ipa", neural=True)
100100

101101
def test_properties(self):
@@ -131,11 +131,11 @@ def test_graph_properties(self):
131131
tg.debugger = [["spam", "spam", "spam", "spam"]]
132132
assert 1 == len(tg.debugger)
133133
assert 4 == len(tg.debugger[0])
134-
with self.assertRaises(ValueError):
134+
with raises(ValueError):
135135
tg.input_nodes = ("foo", "bar", "baz")
136-
with self.assertRaises(ValueError):
136+
with raises(ValueError):
137137
tg.output_nodes = ("foo", "bar", "baz")
138-
with self.assertRaises(ValueError):
138+
with raises(ValueError):
139139
tg.tiers = ["spam", "spam", "eggs", "spam"]
140140
tg = self.test_trans("abab")
141141
tg += tg
@@ -160,15 +160,15 @@ def test_composite_graph_properties(self):
160160
assert [(0 == "b"), (1, "b"), (2, "b"), (3, "b")], ctg.input_nodes
161161
ctg.output_string = "baba"
162162
assert [(0 == "b"), (1, "a"), (2, "b"), (3, "a")], ctg.output_nodes
163-
with self.assertRaises(ValueError):
163+
with raises(ValueError):
164164
ctg.debugger = [["spam", "spam", "spam", "spam"]]
165-
with self.assertRaises(ValueError):
165+
with raises(ValueError):
166166
ctg.edges = [(0, 1), (1, 0), (2, 3), (3, 2)]
167-
with self.assertRaises(ValueError):
167+
with raises(ValueError):
168168
ctg.input_nodes = ("foo", "bar", "baz")
169-
with self.assertRaises(ValueError):
169+
with raises(ValueError):
170170
ctg.output_nodes = ("foo", "bar", "baz")
171-
with self.assertRaises(ValueError):
171+
with raises(ValueError):
172172
ctg.tiers = ["spam", "spam", "eggs", "spam"]
173173
ctg = self.test_trans_composite("aba")
174174
ctg += ctg
@@ -244,7 +244,7 @@ def test_case_preservation(self):
244244
# I guess it's arguable what should happen here, but I'll just change case if any of the characters are differently cased
245245
assert transducer("Tlaba").output_string == "\u2144aba"
246246
# case equivalencies that are not the same length cause indexing errors in the current implementation
247-
with self.assertRaises(MalformedMapping):
247+
with raises(MalformedMapping):
248248
Mapping(
249249
rules=[
250250
{"in": "'a", "out": "b"},
@@ -257,7 +257,7 @@ def test_case_preservation(self):
257257
case_equivalencies={"λ": "\u2144\u2144\u2144"},
258258
)
259259

260-
with self.assertRaises(MalformedMapping):
260+
with raises(MalformedMapping):
261261
_ = Mapping(
262262
rules=[{"in": "a", "out": "b"}],
263263
case_sensitive=True,

0 commit comments

Comments
 (0)