|
1 | 1 | from pathlib import Path |
2 | | -import pytest |
3 | 2 | from subprocess import check_output |
| 3 | +import pytest |
| 4 | + |
4 | 5 |
|
5 | 6 | @pytest.fixture(scope='module') |
6 | 7 | def text_file(): |
7 | 8 | file_path = Path('my_text.txt') |
| 9 | + nr_lines, nr_words = 10, 0 |
8 | 10 | with open(file_path, 'w') as out_file: |
9 | | - for line_nr in range(1, 11): |
10 | | - for word_nr in range(1, line_nr + 1): |
11 | | - print('bla', file=out_file, end=' ') |
12 | | - print('', file=out_file) |
13 | | - yield file_path |
| 11 | + for line_nr in range(nr_lines): |
| 12 | + print(' '.join(['bla']*(line_nr + 1)), file=out_file) |
| 13 | + nr_words += line_nr + 1 |
| 14 | + yield file_path, nr_lines, nr_words |
14 | 15 | file_path.unlink() |
15 | 16 |
|
| 17 | + |
16 | 18 | def test_wc_l(text_file): |
17 | | - output = check_output(['wc', '-l', text_file]) |
18 | | - nr_lines = 10 |
| 19 | + path, nr_lines, _ = text_file |
| 20 | + output = check_output(['wc', '-l', path]) |
19 | 21 | lines, _ = output.decode(encoding='utf-8').split() |
20 | 22 | assert int(lines) == nr_lines |
21 | 23 |
|
| 24 | + |
22 | 25 | def test_wc_w(text_file): |
23 | | - output = check_output(['wc', '-w', text_file]) |
24 | | - nr_lines = 10 |
25 | | - nr_words = 0 |
26 | | - for words in range(1, nr_lines + 1): |
27 | | - nr_words += words |
28 | | - lines, _ = output.decode(encoding='utf-8').split() |
29 | | - assert int(lines) == nr_words |
| 26 | + path, _, nr_words = text_file |
| 27 | + output = check_output(['wc', '-w', path]) |
| 28 | + words, _ = output.decode(encoding='utf-8').split() |
| 29 | + assert int(words) == nr_words |
0 commit comments