Skip to content

Commit 77c0441

Browse files
Update readme, add docs for standard tests
1 parent 6969ff7 commit 77c0441

File tree

5 files changed

+102
-13
lines changed

5 files changed

+102
-13
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,34 @@ $ pip install -e .
311311

312312
There are two types of tests:
313313

314-
1. Manually-written tests for some behavior of the library
315-
2. Proto files and JSON inputs for automated tests
314+
1. Standard tests
315+
2. Custom tests
316316

317-
For #2, you can add a new `*.proto` file into the `betterproto/tests` directory along with a sample `*.json` input and it will get automatically picked up.
317+
#### Standard tests
318+
319+
Adding a standard test case is easy.
320+
321+
- Create a new directory `betterproto/tests/inputs/<name>`
322+
- add `<name>.proto` with a message called `Test`
323+
- add `<name>.json` with some test data
324+
325+
It will be picked up automatically when you run `pipenv test`
326+
327+
- See also: [Standard Tests Development Guide](betterproto\tests\standard-tests.md)
328+
329+
#### Custom tests
330+
331+
Custom tests are found in `tests/test_*.py` and are run with pytest.
332+
333+
#### Running
318334

319335
Here's how to run the tests.
320336

321337
```sh
322338
# Generate assets from sample .proto files
323339
$ pipenv run generate
324340

325-
# Run the tests
341+
# Run all tests
326342
$ pipenv run test
327343
```
328344

betterproto/tests/inputs/bool/test.py

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from betterproto.tests.output_betterproto.bool.bool import Test
2+
3+
4+
def test_value():
5+
message = Test()
6+
assert not message.value, "Boolean is False by default"

betterproto/tests/standard-tests.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Standard Tests Development Guide
2+
3+
Standard test cases are found in [betterproto/tests/inputs](inputs), where each subdirectory represents a testcase, that is verified in isolation.
4+
5+
```
6+
inputs/
7+
bool/
8+
double/
9+
int32/
10+
...
11+
```
12+
13+
## Test case directory structure
14+
15+
Each testcase has a `<name>.proto` file with a message called `Test`, a matching `.json` file and optionally a custom test file called `test_*.py`.
16+
17+
```bash
18+
bool/
19+
bool.proto
20+
bool.json
21+
test_bool.py # optional
22+
```
23+
24+
### proto
25+
26+
`<name>.proto` &mdash; *The protobuf message to test*
27+
28+
```protobuf
29+
syntax = "proto3";
30+
31+
message Test {
32+
bool value = 1;
33+
}
34+
```
35+
36+
You can add multiple `.proto` files to the test case, as long as one file matches the directory name.
37+
38+
### json
39+
40+
`<name>.json` &mdash; *Test-data to validate the message with*
41+
42+
```json
43+
{
44+
"value": true
45+
}
46+
```
47+
48+
### pytest
49+
50+
`test_<name>.py` &mdash; *Custom test to validate specific aspects of the generated class*
51+
52+
```python
53+
from betterproto.tests.output_betterproto.bool.bool import Test
54+
55+
def test_value():
56+
message = Test()
57+
assert not message.value, "Boolean is False by default"
58+
```
59+
60+
## Standard tests
61+
62+
The following tests are automatically executed for all cases:
63+
64+
- [x] Can the generated python code imported?
65+
- [x] Can the generated message class be instantiated?
66+
- [x] Is the generated code compatible with the Google's `grpc_tools.protoc` implementation?
67+
68+
## Running the tests
69+
70+
- `pipenv run generate`
71+
This generates
72+
- `betterproto/tests/output_betterproto` &mdash; *the plugin generated python classes*
73+
- `betterproto/tests/output_reference` &mdash; *reference implementation classes*
74+
- `pipenv run test`
75+

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[pytest]
2-
python_files = test*.py
2+
python_files = test_*.py
33
python_classes =
44
norecursedirs = **/output_*
55
addopts = -p no:warnings

0 commit comments

Comments
 (0)