Skip to content

Commit a942f5c

Browse files
Jayman2000adrienverge
authored andcommitted
Rename tests.common.test_codec_built_in_equivalent()
Some of the functions in the test package are automatically run by test frameworks and some functions in the test package are not. How do test frameworks determine which functions should get run automatically? The answer to that question depends on what test framework you are using. Python’s built-in unittest framework uses the unittest.TestCase class to figure out which which function should get run automatically. You have to put your functions in subclasses or instances of the unittest.TestCase class or else unittest won’t automatically run your code [1]. The pytest framework works differently. By default, pytest will still look for unittest.TestCase subclasses and instances, but it will also run functions with names that begin with “test” even if those functions aren’t inside any classes or instances [2]. When I had first written the test_codec_built_in_equivalent() function, I had only tested it with the unittest framework. The unittest framework does not run that function automatically because it’s not inside a unittest.TestCase subclass or instance. The pytest framework, on the other hand, will try to run test_codec_built_in_equivalent() automatically because it’s name starts with “test”. test_codec_built_in_equivalent() has one mandatory parameter, but pytest doesn’t know what value to use for that parameter, so pytest will fail to run that function. This change prevents pytest from failing to run that function by renaming it to built_in_equivalent_of_test_codec(). The function was never supposed to be run automatically anyway. Fixes #734. [1]: <https://docs.python.org/3/library/unittest.html#organizing-test-code> [2]: <https://docs.pytest.org/en/stable/explanation/goodpractices.html#conventions-for-python-test-discovery>
1 parent 639a7c6 commit a942f5c

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

tests/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def is_test_codec(codec):
104104
return codec in test_codec_infos.keys()
105105

106106

107-
def test_codec_built_in_equivalent(test_codec):
107+
def built_in_equivalent_of_test_codec(test_codec):
108108
return_value = test_codec
109109
for suffix in ('_sig', '_be', '_le'):
110110
return_value = return_value.replace(suffix, '')

tests/test_decoder.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
from tests.common import (
2222
UTF_CODECS,
23+
built_in_equivalent_of_test_codec,
2324
encoding_detectable,
2425
is_test_codec,
2526
register_test_codecs,
2627
temp_workspace,
2728
temp_workspace_with_files_in_many_codecs,
28-
test_codec_built_in_equivalent,
2929
unregister_test_codecs,
3030
uses_bom,
3131
)
@@ -257,23 +257,23 @@ def test_is_test_codec(self):
257257
self.assertFalse(is_test_codec('utf_8'))
258258
self.assertFalse(is_test_codec('utf_8_be'))
259259

260-
def test_test_codec_built_in_equivalent(self):
260+
def test_built_in_equivalent_of_test_codec(self):
261261
self.assertEqual(
262262
'utf_32',
263-
test_codec_built_in_equivalent('utf_32_be_sig')
263+
built_in_equivalent_of_test_codec('utf_32_be_sig')
264264
)
265265
self.assertEqual(
266266
'utf_32',
267-
test_codec_built_in_equivalent('utf_32_le_sig')
267+
built_in_equivalent_of_test_codec('utf_32_le_sig')
268268
)
269269

270270
self.assertEqual(
271271
'utf_16',
272-
test_codec_built_in_equivalent('utf_16_be_sig')
272+
built_in_equivalent_of_test_codec('utf_16_be_sig')
273273
)
274274
self.assertEqual(
275275
'utf_16',
276-
test_codec_built_in_equivalent('utf_16_le_sig')
276+
built_in_equivalent_of_test_codec('utf_16_le_sig')
277277
)
278278

279279
def test_uses_bom(self):
@@ -386,7 +386,7 @@ def test_detect_encoding_with_strings_encoded_at_runtime(self):
386386
elif not encoding_detectable(string, codec):
387387
expected_codec = None
388388
elif is_test_codec(codec):
389-
expected_codec = test_codec_built_in_equivalent(codec)
389+
expected_codec = built_in_equivalent_of_test_codec(codec)
390390
else:
391391
expected_codec = codec
392392
self.detect_encoding_test_helper(

0 commit comments

Comments
 (0)