Refactor test suite to be more readable?
See original GitHub issueWhile working on #174, I also worked on the test suite. In there we have the ginormous tests that are hard to parse, because they do so many things at the same time:
I was wondering if there is a reason for that. Can’t we split this into multiple smaller ones? Utilizing pytest
, placing the following class in the test module is equivalent to the test above:
class TestLineReader:
@pytest.fixture
def text1(self):
return "Line1\nLine2"
@pytest.fixture
def text2(self):
return "Line2,1\nLine2,2\nLine2,3"
def test_functional_read_lines_correctly(self, text1, text2):
source_dp = IterableWrapper([("file1", io.StringIO(text1)), ("file2", io.StringIO(text2))])
line_reader_dp = source_dp.readlines()
expected_result = [("file1", line) for line in text1.split("\n")] + [
("file2", line) for line in text2.split("\n")
]
assert expected_result == list(line_reader_dp)
def test_functional_strip_new_lines_for_bytes(self, text1, text2):
source_dp = IterableWrapper(
[("file1", io.BytesIO(text1.encode("utf-8"))), ("file2", io.BytesIO(text2.encode("utf-8")))]
)
line_reader_dp = source_dp.readlines()
expected_result_bytes = [("file1", line.encode("utf-8")) for line in text1.split("\n")] + [
("file2", line.encode("utf-8")) for line in text2.split("\n")
]
assert expected_result_bytes == list(line_reader_dp)
def test_functional_do_not_strip_newlines(self, text1, text2):
source_dp = IterableWrapper([("file1", io.StringIO(text1)), ("file2", io.StringIO(text2))])
line_reader_dp = source_dp.readlines(strip_newline=False)
expected_result = [
("file1", "Line1\n"),
("file1", "Line2"),
("file2", "Line2,1\n"),
("file2", "Line2,2\n"),
("file2", "Line2,3"),
]
assert expected_result == list(line_reader_dp)
def test_reset(self, text1, text2):
source_dp = IterableWrapper([("file1", io.StringIO(text1)), ("file2", io.StringIO(text2))])
line_reader_dp = LineReader(source_dp, strip_newline=False)
expected_result = [
("file1", "Line1\n"),
("file1", "Line2"),
("file2", "Line2,1\n"),
("file2", "Line2,2\n"),
("file2", "Line2,3"),
]
n_elements_before_reset = 2
res_before_reset, res_after_reset = reset_after_n_next_calls(line_reader_dp, n_elements_before_reset)
assert expected_result[:n_elements_before_reset] == res_before_reset
assert expected_result == res_after_reset
def test_len(self, text1, text2):
source_dp = IterableWrapper([("file1", io.StringIO(text1)), ("file2", io.StringIO(text2))])
line_reader_dp = LineReader(source_dp, strip_newline=False)
with pytest.raises(TypeError, match="has no len"):
len(line_reader_dp)
This is a lot more readable, since we now actually have 5 separate test cases that can individually fail. Plus, while writing this I also found that test_reset
and test_len
were somewhat dependent on test_functional_do_not_strip_newlines
since they don’t neither define line_reader_dp
nor expected_result
themselves.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to refactor code to be more testable - Medium
To prove the correctness of the code, you have to run a set of tests to see what the valIncrementer function gives you...
Read more >3 Easy Steps to Refactoring Tests for Greater Clarity
Choosing good method names makes tests more readable. A good test expresses what is important, and hides what is unimportant.
Read more >Is duplicated code more tolerable in unit tests? - Stack Overflow
It seems there is a trade-off between tests' readability and maintainability. If I leave duplicated code in unit tests, they're more readable, ...
Read more >Code Refactoring: What You Need to Know About It
As a tester, refactoring of code roughly translates to = in-depth testing + regression testing. · User acceptance tests will be important and ......
Read more >Refactoring: Make this code readable | by Betty LD | Nov, 2022
A good design has good test coverage. What is the relationship between code design and code coverage? The reason is that testable code...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
FWIW, we’ve started something similar in torchtext. See here if you’re interested.
Or even more readable: