cloudru-qa-pytest-notifier 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cloudru_qa_pytest_notifier-1.0.0/.gitignore +26 -0
- cloudru_qa_pytest_notifier-1.0.0/LICENSE +202 -0
- cloudru_qa_pytest_notifier-1.0.0/PKG-INFO +163 -0
- cloudru_qa_pytest_notifier-1.0.0/README.md +138 -0
- cloudru_qa_pytest_notifier-1.0.0/pyproject.toml +67 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/__init__.py +0 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/models.py +30 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/notifiers/__init__.py +20 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/notifiers/base.py +23 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/notifiers/console.py +58 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/notifiers/mattermost.py +84 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/notifiers/telegram.py +78 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/plugin.py +131 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/py.typed +0 -0
- cloudru_qa_pytest_notifier-1.0.0/pytest_notifier/settings.py +72 -0
- cloudru_qa_pytest_notifier-1.0.0/tests/__init__.py +0 -0
- cloudru_qa_pytest_notifier-1.0.0/tests/conftest.py +20 -0
- cloudru_qa_pytest_notifier-1.0.0/tests/test_notifiers.py +87 -0
- cloudru_qa_pytest_notifier-1.0.0/tests/test_plugin.py +107 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/docs/plans/
|
|
2
|
+
.venv/
|
|
3
|
+
.venvs/
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[cod]
|
|
6
|
+
.pytest_cache/
|
|
7
|
+
.mypy_cache/
|
|
8
|
+
.ruff_cache/
|
|
9
|
+
.hypothesis/
|
|
10
|
+
.coverage
|
|
11
|
+
coverage.xml
|
|
12
|
+
htmlcov/
|
|
13
|
+
report*.xml
|
|
14
|
+
allure-results/
|
|
15
|
+
allure-report/
|
|
16
|
+
.allure/
|
|
17
|
+
logs/
|
|
18
|
+
*.log
|
|
19
|
+
.env
|
|
20
|
+
.env.*
|
|
21
|
+
.DS_Store
|
|
22
|
+
dist/
|
|
23
|
+
build/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
/packages/*/api/
|
|
26
|
+
.claude/settings.local.json
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: cloudru-qa-pytest-notifier
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Pytest-плагин для уведомлений о результатах прогона тестов
|
|
5
|
+
Project-URL: Repository, https://github.com/cloud-ru-tech/qa-libraries
|
|
6
|
+
Project-URL: Issues, https://github.com/cloud-ru-tech/qa-libraries/issues
|
|
7
|
+
Author-email: Алексей Кирютин <ayukiryutin@cloud.ru>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Software Development :: Testing
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.13
|
|
19
|
+
Requires-Dist: httpx<0.29,>=0.27.0
|
|
20
|
+
Requires-Dist: pydantic-settings<3,>=2.6.0
|
|
21
|
+
Requires-Dist: pydantic<3,>=2.7.0
|
|
22
|
+
Requires-Dist: pytest<10,>=7.0
|
|
23
|
+
Requires-Dist: rich<16,>=14.2.0
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# pytest-notifier
|
|
27
|
+
|
|
28
|
+
[](https://pypi.org/project/cloudru-qa-pytest-notifier/)
|
|
29
|
+
[](https://pypi.org/project/cloudru-qa-pytest-notifier/)
|
|
30
|
+
[](https://github.com/cloud-ru-tech/qa-libraries/blob/master/LICENSE)
|
|
31
|
+
|
|
32
|
+
English | [Русский](README.ru.md)
|
|
33
|
+
|
|
34
|
+
A pytest plugin that sends notifications about failed tests to different channels (Console, Mattermost, Telegram).
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- Notifications when tests fail.
|
|
39
|
+
- Support for multiple channels at once.
|
|
40
|
+
- Flexible configuration through environment variables or `.env` files.
|
|
41
|
+
- Detailed error information, including tracebacks.
|
|
42
|
+
- Custom messages appended to notifications (for Telegram and Mattermost).
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
Requires Python 3.13 or later.
|
|
47
|
+
|
|
48
|
+
```shell
|
|
49
|
+
pip install cloudru-qa-pytest-notifier
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Import the plugin as the `pytest_notifier` module.
|
|
53
|
+
|
|
54
|
+
## Skills for AI agents
|
|
55
|
+
|
|
56
|
+
This library does not have its own skill yet. If your project has other
|
|
57
|
+
libraries from this collection with [skills for AI agents](https://github.com/cloud-ru-tech/qa-libraries#skills-for-ai-agents),
|
|
58
|
+
select and install them from the project root (requires [`uv`](https://docs.astral.sh/uv/)):
|
|
59
|
+
|
|
60
|
+
```shell
|
|
61
|
+
uvx library-skills
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
For Claude Code, add `--claude`.
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
The plugin activates automatically when running `pytest` if installed.
|
|
69
|
+
|
|
70
|
+
## Configuration
|
|
71
|
+
|
|
72
|
+
Configure it through environment variables. All variables use the `PYTEST_NOTIFIER_` prefix.
|
|
73
|
+
|
|
74
|
+
### Main settings
|
|
75
|
+
|
|
76
|
+
`PYTEST_NOTIFIER_DESTINATIONS` is the main variable specifying notification destinations (a bitmask).
|
|
77
|
+
|
|
78
|
+
#### Calculating DESTINATIONS
|
|
79
|
+
|
|
80
|
+
Add the flags for the required channels:
|
|
81
|
+
|
|
82
|
+
| Channel | Flag value |
|
|
83
|
+
|----------------|:--------------:|
|
|
84
|
+
| **Console** | `1` |
|
|
85
|
+
| **Mattermost** | `2` |
|
|
86
|
+
| **Telegram** | `4` |
|
|
87
|
+
|
|
88
|
+
**Example combinations:**
|
|
89
|
+
|
|
90
|
+
| Combination | Calculation | Result |
|
|
91
|
+
|-----------------------|-----------|:-----------------:|
|
|
92
|
+
| Console only | 1 | `1` |
|
|
93
|
+
| Mattermost only | 2 | `2` |
|
|
94
|
+
| Telegram only | 4 | `4` |
|
|
95
|
+
| Console + Mattermost | 1 + 2 | `3` |
|
|
96
|
+
| Console + Telegram | 1 + 4 | `5` |
|
|
97
|
+
| Mattermost + Telegram | 2 + 4 | `6` |
|
|
98
|
+
| **All channels** | 1 + 2 + 4 | `7` |
|
|
99
|
+
|
|
100
|
+
### Mattermost
|
|
101
|
+
|
|
102
|
+
| Variable | Description |
|
|
103
|
+
|-------------------------------------------------|----------------------------------------------------------------------------|
|
|
104
|
+
| `PYTEST_NOTIFIER_MATTERMOST__WEBHOOK_URL` | Webhook URL for sending messages. |
|
|
105
|
+
| `PYTEST_NOTIFIER_MATTERMOST__USERNAME` | Bot name. |
|
|
106
|
+
| `PYTEST_NOTIFIER_MATTERMOST__CHANNEL` | Channel to send messages to. |
|
|
107
|
+
| `PYTEST_NOTIFIER_MATTERMOST__ICON_EMOJI` | Bot icon emoji (optional, for example `:ghost:`). |
|
|
108
|
+
| `PYTEST_NOTIFIER_MATTERMOST__TIMEOUT_SECONDS` | Request timeout in seconds. Default: 10 seconds. |
|
|
109
|
+
| `PYTEST_NOTIFIER_MATTERMOST__SEND_TRACEBACK` | Whether to send the error traceback: `true` or `false`. Default: `true`. |
|
|
110
|
+
| `PYTEST_NOTIFIER_MATTERMOST__ADDITIONAL_MESSAGE` | Extra message appended to the notification. |
|
|
111
|
+
|
|
112
|
+
### Telegram
|
|
113
|
+
|
|
114
|
+
| Variable | Description |
|
|
115
|
+
|-----------------------------------------------|----------------------------------------------------------------------------|
|
|
116
|
+
| `PYTEST_NOTIFIER_TELEGRAM__BOT_TOKEN` | Your bot's token. |
|
|
117
|
+
| `PYTEST_NOTIFIER_TELEGRAM__CHAT_ID` | Chat or channel ID to send messages to. |
|
|
118
|
+
| `PYTEST_NOTIFIER_TELEGRAM__TIMEOUT_SECONDS` | Request timeout in seconds. Default: 10.0. |
|
|
119
|
+
| `PYTEST_NOTIFIER_TELEGRAM__SEND_TRACEBACK` | Whether to send the error traceback: `true` or `false`. Default: `true`. |
|
|
120
|
+
| `PYTEST_NOTIFIER_TELEGRAM__ADDITIONAL_MESSAGE` | Extra message appended to the notification. |
|
|
121
|
+
|
|
122
|
+
### Example `.env` file
|
|
123
|
+
|
|
124
|
+
```ini
|
|
125
|
+
# Enable Mattermost and Telegram (2 + 4 = 6)
|
|
126
|
+
PYTEST_NOTIFIER_DESTINATIONS=6
|
|
127
|
+
|
|
128
|
+
# Mattermost
|
|
129
|
+
PYTEST_NOTIFIER_MATTERMOST__WEBHOOK_URL=https://mattermost.example.com/hooks/xxx
|
|
130
|
+
PYTEST_NOTIFIER_MATTERMOST__USERNAME=TestBot
|
|
131
|
+
PYTEST_NOTIFIER_MATTERMOST__CHANNEL=qa-alerts
|
|
132
|
+
PYTEST_NOTIFIER_MATTERMOST__ADDITIONAL_MESSAGE="Documentation: https://docs.example.com"
|
|
133
|
+
|
|
134
|
+
# Telegram
|
|
135
|
+
PYTEST_NOTIFIER_TELEGRAM__BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz
|
|
136
|
+
PYTEST_NOTIFIER_TELEGRAM__CHAT_ID=-1001234567890
|
|
137
|
+
PYTEST_NOTIFIER_TELEGRAM__SEND_TRACEBACK=false
|
|
138
|
+
PYTEST_NOTIFIER_TELEGRAM__ADDITIONAL_MESSAGE="Contact the QA team for details"
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Development
|
|
142
|
+
|
|
143
|
+
The plugin is developed in the [qa-libraries](https://github.com/cloud-ru-tech/qa-libraries) monorepo
|
|
144
|
+
alongside the other `cloudru-qa-*` libraries. You need [`uv`](https://docs.astral.sh/uv/), GNU Make,
|
|
145
|
+
and [`checkmake`](https://github.com/checkmake/checkmake).
|
|
146
|
+
Run commands from the repository root:
|
|
147
|
+
|
|
148
|
+
```shell
|
|
149
|
+
make sync # install dependencies for all libraries from uv.lock
|
|
150
|
+
make test-pytest-notifier # tests for this plugin
|
|
151
|
+
make typecheck-pytest-notifier # mypy for this plugin
|
|
152
|
+
make check # formatting, ruff, mypy, and tests for the entire workspace
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The plugin loads in every monorepo test run, so `make` always sets
|
|
156
|
+
`PYTEST_NOTIFIER_DESTINATIONS=1`: notifications go only to the console, even if `.env`
|
|
157
|
+
configures Mattermost or Telegram.
|
|
158
|
+
|
|
159
|
+
Tests run on Python 3.13 and 3.14. Create a separate environment for another version:
|
|
160
|
+
`make sync test-pytest-notifier PYTHON_VERSION=3.14`.
|
|
161
|
+
|
|
162
|
+
Publish a release with the git tag `pytest-notifier/v<version>`. The tag version must match the version
|
|
163
|
+
in `packages/pytest-notifier/pyproject.toml`.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# pytest-notifier
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/cloudru-qa-pytest-notifier/)
|
|
4
|
+
[](https://pypi.org/project/cloudru-qa-pytest-notifier/)
|
|
5
|
+
[](https://github.com/cloud-ru-tech/qa-libraries/blob/master/LICENSE)
|
|
6
|
+
|
|
7
|
+
English | [Русский](README.ru.md)
|
|
8
|
+
|
|
9
|
+
A pytest plugin that sends notifications about failed tests to different channels (Console, Mattermost, Telegram).
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Notifications when tests fail.
|
|
14
|
+
- Support for multiple channels at once.
|
|
15
|
+
- Flexible configuration through environment variables or `.env` files.
|
|
16
|
+
- Detailed error information, including tracebacks.
|
|
17
|
+
- Custom messages appended to notifications (for Telegram and Mattermost).
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
Requires Python 3.13 or later.
|
|
22
|
+
|
|
23
|
+
```shell
|
|
24
|
+
pip install cloudru-qa-pytest-notifier
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Import the plugin as the `pytest_notifier` module.
|
|
28
|
+
|
|
29
|
+
## Skills for AI agents
|
|
30
|
+
|
|
31
|
+
This library does not have its own skill yet. If your project has other
|
|
32
|
+
libraries from this collection with [skills for AI agents](https://github.com/cloud-ru-tech/qa-libraries#skills-for-ai-agents),
|
|
33
|
+
select and install them from the project root (requires [`uv`](https://docs.astral.sh/uv/)):
|
|
34
|
+
|
|
35
|
+
```shell
|
|
36
|
+
uvx library-skills
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
For Claude Code, add `--claude`.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
The plugin activates automatically when running `pytest` if installed.
|
|
44
|
+
|
|
45
|
+
## Configuration
|
|
46
|
+
|
|
47
|
+
Configure it through environment variables. All variables use the `PYTEST_NOTIFIER_` prefix.
|
|
48
|
+
|
|
49
|
+
### Main settings
|
|
50
|
+
|
|
51
|
+
`PYTEST_NOTIFIER_DESTINATIONS` is the main variable specifying notification destinations (a bitmask).
|
|
52
|
+
|
|
53
|
+
#### Calculating DESTINATIONS
|
|
54
|
+
|
|
55
|
+
Add the flags for the required channels:
|
|
56
|
+
|
|
57
|
+
| Channel | Flag value |
|
|
58
|
+
|----------------|:--------------:|
|
|
59
|
+
| **Console** | `1` |
|
|
60
|
+
| **Mattermost** | `2` |
|
|
61
|
+
| **Telegram** | `4` |
|
|
62
|
+
|
|
63
|
+
**Example combinations:**
|
|
64
|
+
|
|
65
|
+
| Combination | Calculation | Result |
|
|
66
|
+
|-----------------------|-----------|:-----------------:|
|
|
67
|
+
| Console only | 1 | `1` |
|
|
68
|
+
| Mattermost only | 2 | `2` |
|
|
69
|
+
| Telegram only | 4 | `4` |
|
|
70
|
+
| Console + Mattermost | 1 + 2 | `3` |
|
|
71
|
+
| Console + Telegram | 1 + 4 | `5` |
|
|
72
|
+
| Mattermost + Telegram | 2 + 4 | `6` |
|
|
73
|
+
| **All channels** | 1 + 2 + 4 | `7` |
|
|
74
|
+
|
|
75
|
+
### Mattermost
|
|
76
|
+
|
|
77
|
+
| Variable | Description |
|
|
78
|
+
|-------------------------------------------------|----------------------------------------------------------------------------|
|
|
79
|
+
| `PYTEST_NOTIFIER_MATTERMOST__WEBHOOK_URL` | Webhook URL for sending messages. |
|
|
80
|
+
| `PYTEST_NOTIFIER_MATTERMOST__USERNAME` | Bot name. |
|
|
81
|
+
| `PYTEST_NOTIFIER_MATTERMOST__CHANNEL` | Channel to send messages to. |
|
|
82
|
+
| `PYTEST_NOTIFIER_MATTERMOST__ICON_EMOJI` | Bot icon emoji (optional, for example `:ghost:`). |
|
|
83
|
+
| `PYTEST_NOTIFIER_MATTERMOST__TIMEOUT_SECONDS` | Request timeout in seconds. Default: 10 seconds. |
|
|
84
|
+
| `PYTEST_NOTIFIER_MATTERMOST__SEND_TRACEBACK` | Whether to send the error traceback: `true` or `false`. Default: `true`. |
|
|
85
|
+
| `PYTEST_NOTIFIER_MATTERMOST__ADDITIONAL_MESSAGE` | Extra message appended to the notification. |
|
|
86
|
+
|
|
87
|
+
### Telegram
|
|
88
|
+
|
|
89
|
+
| Variable | Description |
|
|
90
|
+
|-----------------------------------------------|----------------------------------------------------------------------------|
|
|
91
|
+
| `PYTEST_NOTIFIER_TELEGRAM__BOT_TOKEN` | Your bot's token. |
|
|
92
|
+
| `PYTEST_NOTIFIER_TELEGRAM__CHAT_ID` | Chat or channel ID to send messages to. |
|
|
93
|
+
| `PYTEST_NOTIFIER_TELEGRAM__TIMEOUT_SECONDS` | Request timeout in seconds. Default: 10.0. |
|
|
94
|
+
| `PYTEST_NOTIFIER_TELEGRAM__SEND_TRACEBACK` | Whether to send the error traceback: `true` or `false`. Default: `true`. |
|
|
95
|
+
| `PYTEST_NOTIFIER_TELEGRAM__ADDITIONAL_MESSAGE` | Extra message appended to the notification. |
|
|
96
|
+
|
|
97
|
+
### Example `.env` file
|
|
98
|
+
|
|
99
|
+
```ini
|
|
100
|
+
# Enable Mattermost and Telegram (2 + 4 = 6)
|
|
101
|
+
PYTEST_NOTIFIER_DESTINATIONS=6
|
|
102
|
+
|
|
103
|
+
# Mattermost
|
|
104
|
+
PYTEST_NOTIFIER_MATTERMOST__WEBHOOK_URL=https://mattermost.example.com/hooks/xxx
|
|
105
|
+
PYTEST_NOTIFIER_MATTERMOST__USERNAME=TestBot
|
|
106
|
+
PYTEST_NOTIFIER_MATTERMOST__CHANNEL=qa-alerts
|
|
107
|
+
PYTEST_NOTIFIER_MATTERMOST__ADDITIONAL_MESSAGE="Documentation: https://docs.example.com"
|
|
108
|
+
|
|
109
|
+
# Telegram
|
|
110
|
+
PYTEST_NOTIFIER_TELEGRAM__BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz
|
|
111
|
+
PYTEST_NOTIFIER_TELEGRAM__CHAT_ID=-1001234567890
|
|
112
|
+
PYTEST_NOTIFIER_TELEGRAM__SEND_TRACEBACK=false
|
|
113
|
+
PYTEST_NOTIFIER_TELEGRAM__ADDITIONAL_MESSAGE="Contact the QA team for details"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Development
|
|
117
|
+
|
|
118
|
+
The plugin is developed in the [qa-libraries](https://github.com/cloud-ru-tech/qa-libraries) monorepo
|
|
119
|
+
alongside the other `cloudru-qa-*` libraries. You need [`uv`](https://docs.astral.sh/uv/), GNU Make,
|
|
120
|
+
and [`checkmake`](https://github.com/checkmake/checkmake).
|
|
121
|
+
Run commands from the repository root:
|
|
122
|
+
|
|
123
|
+
```shell
|
|
124
|
+
make sync # install dependencies for all libraries from uv.lock
|
|
125
|
+
make test-pytest-notifier # tests for this plugin
|
|
126
|
+
make typecheck-pytest-notifier # mypy for this plugin
|
|
127
|
+
make check # formatting, ruff, mypy, and tests for the entire workspace
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The plugin loads in every monorepo test run, so `make` always sets
|
|
131
|
+
`PYTEST_NOTIFIER_DESTINATIONS=1`: notifications go only to the console, even if `.env`
|
|
132
|
+
configures Mattermost or Telegram.
|
|
133
|
+
|
|
134
|
+
Tests run on Python 3.13 and 3.14. Create a separate environment for another version:
|
|
135
|
+
`make sync test-pytest-notifier PYTHON_VERSION=3.14`.
|
|
136
|
+
|
|
137
|
+
Publish a release with the git tag `pytest-notifier/v<version>`. The tag version must match the version
|
|
138
|
+
in `packages/pytest-notifier/pyproject.toml`.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "cloudru-qa-pytest-notifier"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Pytest-плагин для уведомлений о результатах прогона тестов"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Алексей Кирютин", email = "ayukiryutin@cloud.ru" },
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.13"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"httpx>=0.27.0,<0.29",
|
|
12
|
+
"pydantic>=2.7.0,<3",
|
|
13
|
+
"pydantic-settings>=2.6.0,<3",
|
|
14
|
+
"pytest>=7.0,<10",
|
|
15
|
+
"rich>=14.2.0,<16",
|
|
16
|
+
]
|
|
17
|
+
license = "Apache-2.0"
|
|
18
|
+
license-files = [
|
|
19
|
+
"LICENSE",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Intended Audience :: Developers",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
"Framework :: Pytest",
|
|
28
|
+
"Topic :: Software Development :: Testing",
|
|
29
|
+
"Typing :: Typed",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Repository = "https://github.com/cloud-ru-tech/qa-libraries"
|
|
34
|
+
Issues = "https://github.com/cloud-ru-tech/qa-libraries/issues"
|
|
35
|
+
|
|
36
|
+
[project.entry-points.pytest11]
|
|
37
|
+
pytest_notifier = "pytest_notifier.plugin"
|
|
38
|
+
|
|
39
|
+
[build-system]
|
|
40
|
+
requires = [
|
|
41
|
+
"hatchling>=1.33.0,<2",
|
|
42
|
+
]
|
|
43
|
+
build-backend = "hatchling.build"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = [
|
|
47
|
+
"pytest_notifier",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[tool.hatch.build.targets.sdist]
|
|
51
|
+
include = [
|
|
52
|
+
"/pytest_notifier",
|
|
53
|
+
"/tests",
|
|
54
|
+
"/README.md",
|
|
55
|
+
"/LICENSE",
|
|
56
|
+
"/pyproject.toml",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
61
|
+
testpaths = [
|
|
62
|
+
"tests",
|
|
63
|
+
]
|
|
64
|
+
addopts = "--strict-markers --strict-config"
|
|
65
|
+
filterwarnings = [
|
|
66
|
+
"error",
|
|
67
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass
|
|
5
|
+
class TestFailureInfo:
|
|
6
|
+
"""Information about a failed test or error."""
|
|
7
|
+
|
|
8
|
+
nodeid: str
|
|
9
|
+
name: str
|
|
10
|
+
file_path: str
|
|
11
|
+
error_message: str
|
|
12
|
+
traceback: str | None
|
|
13
|
+
duration: float
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class TestResults:
|
|
18
|
+
"""Test run results."""
|
|
19
|
+
|
|
20
|
+
collected: int
|
|
21
|
+
passed: int
|
|
22
|
+
failed: int
|
|
23
|
+
error: int
|
|
24
|
+
skipped: int
|
|
25
|
+
xfailed: int
|
|
26
|
+
xpassed: int
|
|
27
|
+
exitstatus: int
|
|
28
|
+
exitstatus_text: str
|
|
29
|
+
failures: list[TestFailureInfo] = field(default_factory=list)
|
|
30
|
+
errors: list[TestFailureInfo] = field(default_factory=list)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from pytest_notifier.settings import Destinations
|
|
2
|
+
|
|
3
|
+
from .base import NotifierBase
|
|
4
|
+
from .console import ConsoleNotifier
|
|
5
|
+
from .mattermost import MattermostNotifier
|
|
6
|
+
from .telegram import TelegramNotifier
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"NOTIFIER_REGISTRY",
|
|
10
|
+
"ConsoleNotifier",
|
|
11
|
+
"MattermostNotifier",
|
|
12
|
+
"NotifierBase",
|
|
13
|
+
"TelegramNotifier",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
NOTIFIER_REGISTRY: dict[Destinations, type[NotifierBase]] = {
|
|
17
|
+
Destinations.console: ConsoleNotifier,
|
|
18
|
+
Destinations.mattermost: MattermostNotifier,
|
|
19
|
+
Destinations.telegram: TelegramNotifier,
|
|
20
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
from pytest_notifier.models import TestResults
|
|
4
|
+
from pytest_notifier.settings import Settings
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class NotifierBase(ABC):
|
|
8
|
+
"""Base class for all notifiers."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, settings: Settings) -> None:
|
|
11
|
+
"""Save notification settings.
|
|
12
|
+
|
|
13
|
+
:param settings: channel settings.
|
|
14
|
+
"""
|
|
15
|
+
self.settings = settings
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def send(self, results: TestResults) -> None:
|
|
19
|
+
"""Send a notification about test results."""
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def is_enabled(self) -> bool:
|
|
23
|
+
"""Check whether the notifier is enabled and configured correctly."""
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from rich.console import Console
|
|
2
|
+
from rich.panel import Panel
|
|
3
|
+
from rich.syntax import Syntax
|
|
4
|
+
from rich.table import Table
|
|
5
|
+
|
|
6
|
+
from pytest_notifier.models import TestResults
|
|
7
|
+
|
|
8
|
+
from .base import NotifierBase
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ConsoleNotifier(NotifierBase):
|
|
12
|
+
"""Notifier that outputs results to the terminal."""
|
|
13
|
+
|
|
14
|
+
def is_enabled(self) -> bool:
|
|
15
|
+
"""Check the configured channel availability.
|
|
16
|
+
|
|
17
|
+
:return: whether the settings are sufficient for sending.
|
|
18
|
+
"""
|
|
19
|
+
return True # Terminal notifications are always available.
|
|
20
|
+
|
|
21
|
+
def send(self, results: TestResults) -> None:
|
|
22
|
+
"""Print information about failed tests to the console."""
|
|
23
|
+
console = Console()
|
|
24
|
+
|
|
25
|
+
# Header
|
|
26
|
+
parts = []
|
|
27
|
+
if results.failed:
|
|
28
|
+
parts.append(f"Failed tests: {results.failed}")
|
|
29
|
+
if results.error:
|
|
30
|
+
parts.append(f"Errors: {results.error}")
|
|
31
|
+
|
|
32
|
+
title = f"[bold red]{' | '.join(parts)}[/bold red]"
|
|
33
|
+
console.print(Panel(title, expand=False))
|
|
34
|
+
|
|
35
|
+
all_failures = results.failures + results.errors
|
|
36
|
+
for idx, failure in enumerate(all_failures, 1):
|
|
37
|
+
# Test information
|
|
38
|
+
table = Table(show_header=False, box=None)
|
|
39
|
+
table.add_row("[bold]Test:[/bold]", f"[cyan]{failure.nodeid}[/cyan]")
|
|
40
|
+
table.add_row("[bold]File:[/bold]", failure.file_path)
|
|
41
|
+
table.add_row("[bold]Duration:[/bold]", f"{failure.duration:.2f}s")
|
|
42
|
+
table.add_row("[bold]Error:[/bold]", f"[red]{failure.error_message}[/red]")
|
|
43
|
+
|
|
44
|
+
console.print(f"\n[bold white on red] {idx} [/bold white on red] {failure.name}")
|
|
45
|
+
console.print(table)
|
|
46
|
+
|
|
47
|
+
if failure.traceback:
|
|
48
|
+
console.print("\n[bold]Traceback:[/bold]")
|
|
49
|
+
syntax = Syntax(
|
|
50
|
+
failure.traceback,
|
|
51
|
+
"python",
|
|
52
|
+
theme="monokai",
|
|
53
|
+
line_numbers=True,
|
|
54
|
+
background_color="default",
|
|
55
|
+
)
|
|
56
|
+
console.print(syntax)
|
|
57
|
+
|
|
58
|
+
console.print("\n")
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
|
|
3
|
+
from pytest_notifier.models import TestFailureInfo, TestResults
|
|
4
|
+
from pytest_notifier.settings import Settings
|
|
5
|
+
|
|
6
|
+
from .base import NotifierBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MattermostNotifier(NotifierBase):
|
|
10
|
+
"""Notifier that sends results to Mattermost through a Slack-compatible webhook."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, settings: Settings) -> None:
|
|
13
|
+
"""Save notification settings.
|
|
14
|
+
|
|
15
|
+
:param settings: channel settings.
|
|
16
|
+
"""
|
|
17
|
+
super().__init__(settings)
|
|
18
|
+
self.mm = settings.mattermost
|
|
19
|
+
|
|
20
|
+
def is_enabled(self) -> bool:
|
|
21
|
+
"""Check the configured channel availability.
|
|
22
|
+
|
|
23
|
+
:return: whether the settings are sufficient for sending.
|
|
24
|
+
"""
|
|
25
|
+
return self.mm.webhook_url is not None
|
|
26
|
+
|
|
27
|
+
def send(self, results: TestResults) -> None:
|
|
28
|
+
"""Send results to Mattermost."""
|
|
29
|
+
all_failures = results.failures + results.errors
|
|
30
|
+
for failure in all_failures:
|
|
31
|
+
payload = self._build_failure_payload(failure)
|
|
32
|
+
self._post_to_webhook(payload)
|
|
33
|
+
|
|
34
|
+
def _build_failure_payload(self, failure: TestFailureInfo) -> dict[str, object]:
|
|
35
|
+
"""Create a payload for a specific failed test."""
|
|
36
|
+
payload = self._build_base_payload()
|
|
37
|
+
payload["text"] = "" # All content will be in attachments.
|
|
38
|
+
|
|
39
|
+
pretext = f"Test failed: **{failure.name}**"
|
|
40
|
+
if self.mm.additional_message:
|
|
41
|
+
pretext += "\n"
|
|
42
|
+
pretext += self.mm.additional_message
|
|
43
|
+
|
|
44
|
+
details = [
|
|
45
|
+
f"nodeid: `{failure.nodeid}`",
|
|
46
|
+
f"duration: {failure.duration:.2f}s",
|
|
47
|
+
f"failure:\n```\n{failure.error_message}\n```",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
if failure.traceback and self.mm.send_traceback:
|
|
51
|
+
details.append("")
|
|
52
|
+
details.append("traceback:")
|
|
53
|
+
details.append(f"```\n{failure.traceback}\n```")
|
|
54
|
+
|
|
55
|
+
attachment = {
|
|
56
|
+
"color": "#FF0000",
|
|
57
|
+
"pretext": pretext,
|
|
58
|
+
"text": "\n".join(details),
|
|
59
|
+
"mrkdwn_in": ["pretext", "text"],
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
payload["attachments"] = [attachment]
|
|
63
|
+
|
|
64
|
+
return payload
|
|
65
|
+
|
|
66
|
+
def _build_base_payload(self) -> dict[str, object]:
|
|
67
|
+
"""Create a base payload with shared settings (username, channel, icon)."""
|
|
68
|
+
payload: dict[str, object] = {}
|
|
69
|
+
|
|
70
|
+
if self.mm.username:
|
|
71
|
+
payload["username"] = self.mm.username
|
|
72
|
+
if self.mm.icon_emoji:
|
|
73
|
+
payload["icon_emoji"] = self.mm.icon_emoji
|
|
74
|
+
if self.mm.channel:
|
|
75
|
+
payload["channel"] = self.mm.channel
|
|
76
|
+
|
|
77
|
+
return payload
|
|
78
|
+
|
|
79
|
+
def _post_to_webhook(self, payload: dict[str, object]) -> None:
|
|
80
|
+
url = str(self.mm.webhook_url)
|
|
81
|
+
timeout = self.mm.timeout_seconds
|
|
82
|
+
|
|
83
|
+
with httpx.Client(timeout=timeout) as client:
|
|
84
|
+
client.post(url, json=payload).raise_for_status()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
|
|
3
|
+
from pytest_notifier.models import TestResults
|
|
4
|
+
from pytest_notifier.settings import Settings
|
|
5
|
+
|
|
6
|
+
from .base import NotifierBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TelegramNotifier(NotifierBase):
|
|
10
|
+
"""Notifier that sends results to Telegram through the Bot API."""
|
|
11
|
+
|
|
12
|
+
BASE_URL = "https://api.telegram.org/bot"
|
|
13
|
+
|
|
14
|
+
def __init__(self, settings: Settings) -> None:
|
|
15
|
+
"""Save notification settings.
|
|
16
|
+
|
|
17
|
+
:param settings: channel settings.
|
|
18
|
+
"""
|
|
19
|
+
super().__init__(settings)
|
|
20
|
+
self.tg = settings.telegram
|
|
21
|
+
|
|
22
|
+
def is_enabled(self) -> bool:
|
|
23
|
+
"""Check the configured channel availability.
|
|
24
|
+
|
|
25
|
+
:return: whether the settings are sufficient for sending.
|
|
26
|
+
"""
|
|
27
|
+
return self.tg.bot_token is not None and self.tg.chat_id is not None
|
|
28
|
+
|
|
29
|
+
def send(self, results: TestResults) -> None:
|
|
30
|
+
"""Send messages about failed tests.
|
|
31
|
+
|
|
32
|
+
:param results: run results.
|
|
33
|
+
"""
|
|
34
|
+
all_failures = results.failures + results.errors
|
|
35
|
+
for _, failure in enumerate(all_failures, 1):
|
|
36
|
+
lines = [
|
|
37
|
+
f"Test failed: *{failure.name}*",
|
|
38
|
+
"",
|
|
39
|
+
f"nodeid: `{failure.nodeid}`",
|
|
40
|
+
f"duration: {failure.duration:.2f}s",
|
|
41
|
+
"failure:",
|
|
42
|
+
f"```\n{failure.error_message}\n```",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
if failure.traceback and self.settings.telegram.send_traceback:
|
|
46
|
+
lines.extend(
|
|
47
|
+
[
|
|
48
|
+
"",
|
|
49
|
+
"traceback:",
|
|
50
|
+
f"```\n{failure.traceback}\n```",
|
|
51
|
+
]
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
if self.tg.additional_message:
|
|
55
|
+
lines.extend(
|
|
56
|
+
[
|
|
57
|
+
"",
|
|
58
|
+
self.tg.additional_message,
|
|
59
|
+
]
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
message = "\n".join(lines)
|
|
63
|
+
self._send_message(message)
|
|
64
|
+
|
|
65
|
+
def _send_message(self, text: str) -> None:
|
|
66
|
+
if self.tg.bot_token is None:
|
|
67
|
+
return
|
|
68
|
+
bot_token = self.tg.bot_token.get_secret_value()
|
|
69
|
+
url = f"{self.BASE_URL}{bot_token}/sendMessage"
|
|
70
|
+
|
|
71
|
+
payload = {
|
|
72
|
+
"chat_id": self.tg.chat_id,
|
|
73
|
+
"text": text,
|
|
74
|
+
"parse_mode": "Markdown",
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
with httpx.Client(timeout=self.tg.timeout_seconds) as client:
|
|
78
|
+
client.post(url, json=payload).raise_for_status()
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
from typing import TYPE_CHECKING, cast
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from _pytest._code.code import ExceptionRepr
|
|
6
|
+
|
|
7
|
+
from .models import TestFailureInfo, TestResults
|
|
8
|
+
from .notifiers import NOTIFIER_REGISTRY, NotifierBase
|
|
9
|
+
from .settings import Settings
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from collections.abc import Mapping, Sequence
|
|
13
|
+
|
|
14
|
+
from _pytest.terminal import TerminalReporter
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None:
|
|
18
|
+
"""Pytest hook that sends a result summary to all active channels."""
|
|
19
|
+
settings = Settings()
|
|
20
|
+
results = extract_test_results(session, exitstatus)
|
|
21
|
+
|
|
22
|
+
if results.failed == 0 and results.error == 0:
|
|
23
|
+
return
|
|
24
|
+
|
|
25
|
+
notifiers = get_active_notifiers(settings)
|
|
26
|
+
|
|
27
|
+
for notifier in notifiers:
|
|
28
|
+
with contextlib.suppress(Exception):
|
|
29
|
+
notifier.send(results)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def extract_test_results(session: pytest.Session, exitstatus: int) -> TestResults:
|
|
33
|
+
"""Extract results from pytest.Session into an independent structure."""
|
|
34
|
+
tr = cast("TerminalReporter | None", session.config.pluginmanager.get_plugin("terminalreporter"))
|
|
35
|
+
|
|
36
|
+
passed = failed = error = skipped = xfailed = xpassed = 0
|
|
37
|
+
collected = session.testscollected
|
|
38
|
+
failures: list[TestFailureInfo] = []
|
|
39
|
+
errors: list[TestFailureInfo] = []
|
|
40
|
+
|
|
41
|
+
if tr is not None:
|
|
42
|
+
stats = cast("Mapping[str, Sequence[pytest.TestReport]]", tr.stats)
|
|
43
|
+
passed = len(stats.get("passed", []))
|
|
44
|
+
failed = len(stats.get("failed", []))
|
|
45
|
+
error = len(stats.get("error", []))
|
|
46
|
+
skipped = len(stats.get("skipped", []))
|
|
47
|
+
xfailed = len(stats.get("xfailed", []))
|
|
48
|
+
xpassed = len(stats.get("xpassed", []))
|
|
49
|
+
|
|
50
|
+
failures.extend(extract_failure_info(report) for report in stats.get("failed", []))
|
|
51
|
+
|
|
52
|
+
errors.extend(extract_failure_info(report) for report in stats.get("error", []))
|
|
53
|
+
|
|
54
|
+
exitstatus_text = exitstatus_to_text(exitstatus)
|
|
55
|
+
|
|
56
|
+
return TestResults(
|
|
57
|
+
collected=collected,
|
|
58
|
+
passed=passed,
|
|
59
|
+
failed=failed,
|
|
60
|
+
error=error,
|
|
61
|
+
skipped=skipped,
|
|
62
|
+
xfailed=xfailed,
|
|
63
|
+
xpassed=xpassed,
|
|
64
|
+
exitstatus=exitstatus,
|
|
65
|
+
exitstatus_text=exitstatus_text,
|
|
66
|
+
failures=failures,
|
|
67
|
+
errors=errors,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def get_active_notifiers(settings: Settings) -> list[NotifierBase]:
|
|
72
|
+
"""Return active notifiers based on the destinations flags."""
|
|
73
|
+
notifiers = []
|
|
74
|
+
|
|
75
|
+
for destination, notifier_class in NOTIFIER_REGISTRY.items():
|
|
76
|
+
if settings.destinations & destination:
|
|
77
|
+
notifier = notifier_class(settings)
|
|
78
|
+
if notifier.is_enabled():
|
|
79
|
+
notifiers.append(notifier)
|
|
80
|
+
|
|
81
|
+
return notifiers
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def extract_failure_info(report: pytest.TestReport) -> TestFailureInfo:
|
|
85
|
+
"""Extract error information from TestReport."""
|
|
86
|
+
file_path, _line, test_name = report.location
|
|
87
|
+
error_message = get_short_error_message(report)
|
|
88
|
+
traceback = report.longreprtext
|
|
89
|
+
|
|
90
|
+
return TestFailureInfo(
|
|
91
|
+
nodeid=report.nodeid,
|
|
92
|
+
name=test_name,
|
|
93
|
+
file_path=file_path,
|
|
94
|
+
error_message=error_message,
|
|
95
|
+
traceback=traceback,
|
|
96
|
+
duration=report.duration,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def get_short_error_message(report: pytest.TestReport) -> str:
|
|
101
|
+
"""Extract a short error message from the report."""
|
|
102
|
+
longrepr = report.longrepr
|
|
103
|
+
|
|
104
|
+
if longrepr is None:
|
|
105
|
+
return "Unknown error"
|
|
106
|
+
|
|
107
|
+
if isinstance(longrepr, str):
|
|
108
|
+
return longrepr[:200]
|
|
109
|
+
|
|
110
|
+
reprcrash = longrepr.reprcrash if isinstance(longrepr, ExceptionRepr) else None
|
|
111
|
+
if reprcrash:
|
|
112
|
+
message = reprcrash.message
|
|
113
|
+
if message:
|
|
114
|
+
return message[:200]
|
|
115
|
+
|
|
116
|
+
longreprtext = report.longreprtext
|
|
117
|
+
if longreprtext:
|
|
118
|
+
first_line = longreprtext.split("\n")[0]
|
|
119
|
+
return first_line[:200]
|
|
120
|
+
|
|
121
|
+
return "Unknown error"
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def exitstatus_to_text(exitstatus: int) -> str:
|
|
125
|
+
"""Convert a pytest exit code to text."""
|
|
126
|
+
try:
|
|
127
|
+
code = pytest.ExitCode(exitstatus)
|
|
128
|
+
except ValueError:
|
|
129
|
+
return str(exitstatus)
|
|
130
|
+
else:
|
|
131
|
+
return code.name
|
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from enum import Flag
|
|
3
|
+
|
|
4
|
+
from pydantic import AnyUrl, BaseModel, SecretStr, field_validator
|
|
5
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
6
|
+
|
|
7
|
+
env = os.environ.get("ENV", "dev")
|
|
8
|
+
|
|
9
|
+
DEFAULT_TIMEOUT_SECONDS = 10.0
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Destinations(Flag):
|
|
13
|
+
"""Notification destinations."""
|
|
14
|
+
|
|
15
|
+
console = 0x1
|
|
16
|
+
mattermost = 0x2
|
|
17
|
+
telegram = 0x4
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MattermostSettings(BaseModel):
|
|
21
|
+
"""Mattermost webhook settings."""
|
|
22
|
+
|
|
23
|
+
webhook_url: AnyUrl | None = None
|
|
24
|
+
icon_emoji: str | None = None
|
|
25
|
+
username: str | None = None
|
|
26
|
+
channel: str | None = None
|
|
27
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS
|
|
28
|
+
send_traceback: bool = True
|
|
29
|
+
additional_message: str | None = None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TelegramSettings(BaseModel):
|
|
33
|
+
"""Telegram notification settings."""
|
|
34
|
+
|
|
35
|
+
bot_token: SecretStr | None = None
|
|
36
|
+
chat_id: str | None = None
|
|
37
|
+
timeout_seconds: float = DEFAULT_TIMEOUT_SECONDS
|
|
38
|
+
send_traceback: bool = True
|
|
39
|
+
additional_message: str | None = None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Settings(BaseSettings):
|
|
43
|
+
"""Notification channel settings from the environment."""
|
|
44
|
+
|
|
45
|
+
destinations: Destinations = Destinations.console
|
|
46
|
+
mattermost: MattermostSettings = MattermostSettings()
|
|
47
|
+
telegram: TelegramSettings = TelegramSettings()
|
|
48
|
+
|
|
49
|
+
model_config = SettingsConfigDict(
|
|
50
|
+
extra="ignore",
|
|
51
|
+
env_prefix="PYTEST_NOTIFIER_",
|
|
52
|
+
env_nested_delimiter="__",
|
|
53
|
+
env_file=[".env", f".{env}.env"],
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@field_validator("destinations", mode="before")
|
|
57
|
+
@classmethod
|
|
58
|
+
def parse_destinations(cls, v: object) -> Destinations:
|
|
59
|
+
"""Read the channel bitmask.
|
|
60
|
+
|
|
61
|
+
:param v: integer, numeric string, or an existing mask.
|
|
62
|
+
:return: selected channels.
|
|
63
|
+
:raises ValueError: if the value is not a bitmask.
|
|
64
|
+
"""
|
|
65
|
+
if isinstance(v, Destinations):
|
|
66
|
+
return v
|
|
67
|
+
if isinstance(v, str):
|
|
68
|
+
return Destinations(int(v))
|
|
69
|
+
if isinstance(v, int):
|
|
70
|
+
return Destinations(v)
|
|
71
|
+
message = f"Cannot parse destinations from {v}"
|
|
72
|
+
raise ValueError(message)
|
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
pytest_plugins = ["pytester"]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@pytest.fixture(autouse=True)
|
|
7
|
+
def isolate_nested_plugins(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
8
|
+
"""Load only plugins explicitly passed through ``-p`` in nested pytest runs.
|
|
9
|
+
|
|
10
|
+
All plugins in the suite are installed in the shared workspace environment, and autoloading
|
|
11
|
+
registers them again under different names.
|
|
12
|
+
"""
|
|
13
|
+
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.fixture(autouse=True)
|
|
17
|
+
def isolate_notifications(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
18
|
+
monkeypatch.setenv("PYTEST_NOTIFIER_DESTINATIONS", "1")
|
|
19
|
+
monkeypatch.delenv("PYTEST_NOTIFIER_MATTERMOST__WEBHOOK_URL", raising=False)
|
|
20
|
+
monkeypatch.delenv("PYTEST_NOTIFIER_TELEGRAM__BOT_TOKEN", raising=False)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
import pytest
|
|
3
|
+
from assertpy import assert_that
|
|
4
|
+
from pydantic import AnyUrl, SecretStr
|
|
5
|
+
from pytest_notifier.models import TestFailureInfo as FailureInfo
|
|
6
|
+
from pytest_notifier.models import TestResults as Results
|
|
7
|
+
from pytest_notifier.notifiers.mattermost import MattermostNotifier
|
|
8
|
+
from pytest_notifier.notifiers.telegram import TelegramNotifier
|
|
9
|
+
from pytest_notifier.plugin import get_active_notifiers
|
|
10
|
+
from pytest_notifier.settings import Destinations, MattermostSettings, Settings, TelegramSettings
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@pytest.fixture()
|
|
14
|
+
def requests(monkeypatch: pytest.MonkeyPatch) -> list[httpx.Request]:
|
|
15
|
+
captured: list[httpx.Request] = []
|
|
16
|
+
|
|
17
|
+
def capture(request: httpx.Request) -> httpx.Response:
|
|
18
|
+
captured.append(request)
|
|
19
|
+
return httpx.Response(200)
|
|
20
|
+
|
|
21
|
+
client_class = httpx.Client
|
|
22
|
+
|
|
23
|
+
def client(*, timeout: float) -> httpx.Client:
|
|
24
|
+
return client_class(timeout=timeout, transport=httpx.MockTransport(capture))
|
|
25
|
+
|
|
26
|
+
monkeypatch.setattr(httpx, "Client", client)
|
|
27
|
+
return captured
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.fixture()
|
|
31
|
+
def failure_results() -> Results:
|
|
32
|
+
failure = FailureInfo("test.py::test_cart", "test_cart", "test.py", "stock exhausted", "trace details", 0.25)
|
|
33
|
+
return Results(1, 0, 1, 0, 0, 0, 0, 1, "TESTS_FAILED", failures=[failure])
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_console_is_default_destination(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
37
|
+
monkeypatch.delenv("PYTEST_NOTIFIER_DESTINATIONS")
|
|
38
|
+
|
|
39
|
+
assert_that(Settings().destinations).is_equal_to(Destinations.console)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_numeric_environment_selects_channels(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
43
|
+
monkeypatch.setenv("PYTEST_NOTIFIER_DESTINATIONS", "5")
|
|
44
|
+
|
|
45
|
+
assert_that(Settings().destinations).is_equal_to(Destinations.console | Destinations.telegram)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_skips_remote_channels_without_credentials() -> None:
|
|
49
|
+
settings = Settings(destinations=Destinations.mattermost | Destinations.telegram)
|
|
50
|
+
|
|
51
|
+
assert_that(get_active_notifiers(settings)).is_empty()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_mattermost_posts_failure_details(requests: list[httpx.Request], failure_results: Results) -> None:
|
|
55
|
+
settings = Settings(mattermost=MattermostSettings(webhook_url=AnyUrl("https://notifications.example/hook")))
|
|
56
|
+
|
|
57
|
+
MattermostNotifier(settings).send(failure_results)
|
|
58
|
+
|
|
59
|
+
assert_that(requests[0].content.decode()).contains("test_cart", "stock exhausted", "trace details")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_mattermost_omits_disabled_traceback(requests: list[httpx.Request], failure_results: Results) -> None:
|
|
63
|
+
settings = Settings(
|
|
64
|
+
mattermost=MattermostSettings(webhook_url=AnyUrl("https://hooks.example/results"), send_traceback=False)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
MattermostNotifier(settings).send(failure_results)
|
|
68
|
+
|
|
69
|
+
assert_that(requests[0].content.decode()).does_not_contain("trace details")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_telegram_posts_to_configured_chat(requests: list[httpx.Request], failure_results: Results) -> None:
|
|
73
|
+
settings = Settings(telegram=TelegramSettings(bot_token=SecretStr("fake-token"), chat_id="case-chat"))
|
|
74
|
+
|
|
75
|
+
TelegramNotifier(settings).send(failure_results)
|
|
76
|
+
|
|
77
|
+
assert_that(requests[0].content.decode()).contains("case-chat", "stock exhausted", "trace details")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_telegram_omits_disabled_traceback(requests: list[httpx.Request], failure_results: Results) -> None:
|
|
81
|
+
settings = Settings(
|
|
82
|
+
telegram=TelegramSettings(bot_token=SecretStr("fake-other"), chat_id="other-chat", send_traceback=False)
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
TelegramNotifier(settings).send(failure_results)
|
|
86
|
+
|
|
87
|
+
assert_that(requests[0].content.decode()).does_not_contain("trace details")
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from assertpy import assert_that
|
|
3
|
+
from pytest_notifier.plugin import exitstatus_to_text, extract_failure_info, get_short_error_message
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@pytest.mark.parametrize(
|
|
7
|
+
("status", "expected"),
|
|
8
|
+
[
|
|
9
|
+
(0, "OK"),
|
|
10
|
+
(1, "TESTS_FAILED"),
|
|
11
|
+
(2, "INTERRUPTED"),
|
|
12
|
+
(3, "INTERNAL_ERROR"),
|
|
13
|
+
(4, "USAGE_ERROR"),
|
|
14
|
+
(5, "NO_TESTS_COLLECTED"),
|
|
15
|
+
(71, "71"),
|
|
16
|
+
],
|
|
17
|
+
)
|
|
18
|
+
def test_describes_exit_status(status: int, expected: str) -> None:
|
|
19
|
+
assert_that(exitstatus_to_text(status)).is_equal_to(expected)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_extracts_test_identity_and_failure_details() -> None:
|
|
23
|
+
report = pytest.TestReport(
|
|
24
|
+
nodeid="suite/test_api.py::test_fetch",
|
|
25
|
+
location=("suite/test_api.py", 18, "test_fetch"),
|
|
26
|
+
keywords=dict[str, object](),
|
|
27
|
+
outcome="failed",
|
|
28
|
+
longrepr="unexpected response",
|
|
29
|
+
when="call",
|
|
30
|
+
duration=0.375,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
failure = extract_failure_info(report)
|
|
34
|
+
|
|
35
|
+
assert_that(failure).has_nodeid(report.nodeid).has_name("test_fetch").has_file_path(
|
|
36
|
+
"suite/test_api.py"
|
|
37
|
+
).has_error_message("unexpected response").has_duration(0.375)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_limits_short_failure_message() -> None:
|
|
41
|
+
report = pytest.TestReport(
|
|
42
|
+
"test.py::test_big", ("test.py", 0, "test_big"), dict[str, object](), "failed", "x" * 321, "call"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
assert_that(get_short_error_message(report)).is_equal_to("x" * 200)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_reports_unknown_error_when_representation_is_missing() -> None:
|
|
49
|
+
report = pytest.TestReport(
|
|
50
|
+
"test.py::test_empty", ("test.py", 0, "test_empty"), dict[str, object](), "failed", None, "call"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
assert_that(get_short_error_message(report)).is_equal_to("Unknown error")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_successful_session_sends_no_notification(pytester: pytest.Pytester) -> None:
|
|
57
|
+
pytester.makepyfile("def test_success():\n pass\n")
|
|
58
|
+
|
|
59
|
+
result = pytester.runpytest_subprocess(
|
|
60
|
+
"-p",
|
|
61
|
+
"no:pytest_notifier",
|
|
62
|
+
"-p",
|
|
63
|
+
"pytest_notifier.plugin",
|
|
64
|
+
"-p",
|
|
65
|
+
"pytest_asyncio.plugin",
|
|
66
|
+
"-o",
|
|
67
|
+
"asyncio_default_fixture_loop_scope=function",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
assert_that(result.stdout.str()).does_not_contain("Failed tests:")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_failed_session_prints_failure_notification(pytester: pytest.Pytester) -> None:
|
|
74
|
+
pytester.makepyfile('def test_failure():\n raise ValueError("specific failure")\n')
|
|
75
|
+
|
|
76
|
+
result = pytester.runpytest_subprocess(
|
|
77
|
+
"-p",
|
|
78
|
+
"no:pytest_notifier",
|
|
79
|
+
"-p",
|
|
80
|
+
"pytest_notifier.plugin",
|
|
81
|
+
"-p",
|
|
82
|
+
"pytest_asyncio.plugin",
|
|
83
|
+
"-o",
|
|
84
|
+
"asyncio_default_fixture_loop_scope=function",
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
assert_that(result.stdout.str()).contains("Failed tests: 1", "specific failure")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_failed_setup_prints_error_notification(pytester: pytest.Pytester) -> None:
|
|
91
|
+
pytester.makepyfile(
|
|
92
|
+
"import pytest\n@pytest.fixture()\ndef broken():\n"
|
|
93
|
+
' raise RuntimeError("setup unavailable")\ndef test_error(broken):\n pass\n'
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
result = pytester.runpytest_subprocess(
|
|
97
|
+
"-p",
|
|
98
|
+
"no:pytest_notifier",
|
|
99
|
+
"-p",
|
|
100
|
+
"pytest_notifier.plugin",
|
|
101
|
+
"-p",
|
|
102
|
+
"pytest_asyncio.plugin",
|
|
103
|
+
"-o",
|
|
104
|
+
"asyncio_default_fixture_loop_scope=function",
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
assert_that(result.stdout.str()).contains("Errors: 1", "setup unavailable")
|