3
3
import subprocess
4
4
import sys
5
5
import uuid
6
+ import pytest
6
7
7
8
from os .path import join , getsize
8
9
9
10
10
- def test_examples_in_docs ():
11
+ examples_in_docs_data = []
11
12
13
+ def _get_examples_in_docs_data ():
14
+ global examples_in_docs_data
15
+ examples_in_docs_data = []
16
+ for root , dirs , files in os .walk ('examples' ):
17
+ for filename in files :
18
+ if 'xlsx' in root and sys .version_info [:2 ] < (3 ,4 ):
19
+ continue
20
+ if 'cmd.txt' in filename :
21
+ examples_in_docs_data .append ((root , filename ))
22
+
23
+ _get_examples_in_docs_data ()
24
+
25
+
26
+ def test_examples_receipt ():
12
27
with open ('examples/receipt/source-map/expected.json' , 'rb' ) as fp :
13
28
expected = fp .read ()
14
29
for expected_filename in [
15
30
'normalised/expected.json' ,
16
31
'combine-table-into-cafe/expected.json' ,
17
32
'combine-table-into-cafe-2/expected.json' ,
18
33
]:
19
- with open ('examples/receipt/' + expected_filename , 'rb' ) as fp2 :
20
- assert fp2 .read () == expected , "Files differ: examples/receipt/source-map/expected.json, examples/receipt/{}" .format (expected_filename )
21
- tests_passed = 0
22
- for root , dirs , files in os .walk ('examples' ):
23
- for filename in files :
24
- if 'xlsx' in root and sys .version_info [:2 ] < (3 ,4 ):
25
- continue
26
- if 'cmd.txt' in filename :
34
+ with open ('examples/receipt/' + expected_filename , 'rb' ) as fp2 :
35
+ assert fp2 .read () == expected , "Files differ: examples/receipt/source-map/expected.json, examples/receipt/{}" .format (
36
+ expected_filename )
37
+
38
+
39
+ @pytest .mark .parametrize ("root, filename" , examples_in_docs_data )
40
+ def test_example_in_doc (root , filename ):
27
41
if os .path .exists (join (root , 'actual' )) and os .path .isdir (join (root , 'actual' )):
28
42
os .rename (join (root , 'actual' ), join (root , 'actual.' + str (uuid .uuid4 ())))
29
43
os .mkdir (join (root , 'actual' ))
@@ -95,9 +109,11 @@ def test_examples_in_docs():
95
109
else :
96
110
expected_stderr += b'\n '
97
111
assert _simplify_warnings (_strip (actual_stderr )) == _simplify_warnings (_strip (expected_stderr )), "Different stderr: {}" .format (cmds )
98
- tests_passed += 1
99
- # Check that the number of tests were run that we expected
100
- assert tests_passed == 56
112
+
113
+
114
+ def test_expected_number_of_examples_in_docs_data ():
115
+ assert len (examples_in_docs_data ) == 56
116
+
101
117
102
118
def _simplify_warnings (lines ):
103
119
return '\n ' .join ([_simplify_line (line ) for line in lines .split ('\n ' )])
@@ -117,4 +133,6 @@ def _strip(output):
117
133
118
134
# Useful for a coverage check - see developer docs for how to run the check
119
135
if __name__ == '__main__' :
120
- test_examples_in_docs ()
136
+ test_examples_receipt ()
137
+ for root , filename in examples_in_docs_data :
138
+ test_example_in_doc (root , filename )
0 commit comments