-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
150 lines (103 loc) · 3.59 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Unit tests using pytest and pytest-flask
https://pytest-flask.readthedocs.io/en/latest/
"""
import logging
import os
import pathlib
import sys
import types
import tests.util.sample
# import pandas as pd
import pytest
# import flask.testing
# flask.testing is used indirectly in a fixture, which PyCharm can't see. This adds
# a no-op usage, that PyCharm can see.
# x=flask.testing
import lxml.etree
import webapp.run
logging.getLogger('matplotlib').setLevel(logging.ERROR)
PROJ_ROOT = pathlib.Path(__file__).parent.resolve()
TEST_DOCS = PROJ_ROOT / 'tests/test_docs'
# Hooks
def pytest_addoption(parser):
parser.addoption(
'--sample-error',
dest='sample_error',
action='store_true',
default=False,
help='Handle sample mismatch as test failure instead of opening diff viewer',
)
parser.addoption(
'--sample-update',
dest='sample_update',
action='store_true',
default=False,
help='Update mismatched samples instead of opening diff viewer',
)
# @pytest.fixture(autouse=True)
# def sample_options(request):
# return types.SimpleNamespace(
# error=request.config.getoption("--sample-error"),
# update=request.config.getoption("--sample-update"),
# )
def pytest_configure(config):
"""Allow plugins and conftest files to perform initial configuration
This hook is called for every plugin and initial conftest file after command line
options have been parsed.
After that, the hook is called for other conftest files as they are imported.
"""
sys.is_running_under_travis = "TRAVIS" in os.environ
sys.is_running_under_pytest = True
tests.util.sample.options = types.SimpleNamespace(
error=config.getoption("--sample-error"),
update=config.getoption("--sample-update"),
)
# Only accept error messages from loggers that are noisy at debug.
logging.getLogger('django.db.backends.schema').setLevel(logging.ERROR)
# Implicit fixtures (autouse = True)
@pytest.fixture(scope='function', autouse=True)
def expose_errors(config):
"""Disable automatic error handling during request."""
config['TESTING'] = True
# Flask fixtures
@pytest.fixture
def app():
return webapp.run.app
# Test files
@pytest.fixture
def tmpdir(tmpdir) -> pathlib.Path:
return pathlib.Path(tmpdir)
@pytest.fixture
def docs_path() -> pathlib.Path:
return TEST_DOCS
@pytest.fixture
def complete_eml() -> lxml.etree.ElementTree:
"""A complete sample EML doc.
Generated from the EML 2.2.0 XML Schema with Oxygen, limited to a depth of 20
levels. Then hand edited to add sample text and schemaLocations.
"""
eml_path = TEST_DOCS / 'complete_eml.xml'
return lxml.etree.parse(eml_path.as_posix())
@pytest.fixture
def docbook_xml() -> lxml.etree.ElementTree:
"""A sample DocBook doc."""
xml_path = TEST_DOCS / 'docbook.xml'
return lxml.etree.parse(xml_path.as_posix())
@pytest.fixture
def eml_with_complex_docbook() -> lxml.etree.ElementTree:
"""A EML doc with extensive DocBook structures.
Found with:
$ cd dex-data/___data
$ rg -c '<section' | sort -nk2 -t:
"""
eml_path = TEST_DOCS / 'knb-lter-cap.661.2.eml.xml'
return lxml.etree.parse(eml_path.as_posix())
@pytest.fixture
def docbook_and_markdown() -> str:
"""An XML doc with a mix of DocBook and markdown elements."""
xml_path = TEST_DOCS / 'docbook_and_markdown.xml'
return lxml.etree.parse(xml_path.as_posix())
@pytest.fixture
def markdown_table_md() -> str:
"""A simple markdown table."""
return (TEST_DOCS / 'markdown_table.md').read_text()