Skip to content

Commit

Permalink
Update readme, add docs for standard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boukeversteegh committed May 22, 2020
1 parent 6969ff7 commit 77c0441
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 13 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,34 @@ $ pip install -e .

There are two types of tests:

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

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.
#### Standard tests

Adding a standard test case is easy.

- Create a new directory `betterproto/tests/inputs/<name>`
- add `<name>.proto` with a message called `Test`
- add `<name>.json` with some test data

It will be picked up automatically when you run `pipenv test`

- See also: [Standard Tests Development Guide](betterproto\tests\standard-tests.md)

#### Custom tests

Custom tests are found in `tests/test_*.py` and are run with pytest.

#### Running

Here's how to run the tests.

```sh
# Generate assets from sample .proto files
$ pipenv run generate

# Run the tests
# Run all tests
$ pipenv run test
```

Expand Down
8 changes: 0 additions & 8 deletions betterproto/tests/inputs/bool/test.py

This file was deleted.

6 changes: 6 additions & 0 deletions betterproto/tests/inputs/bool/test_bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from betterproto.tests.output_betterproto.bool.bool import Test


def test_value():
message = Test()
assert not message.value, "Boolean is False by default"
75 changes: 75 additions & 0 deletions betterproto/tests/standard-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Standard Tests Development Guide

Standard test cases are found in [betterproto/tests/inputs](inputs), where each subdirectory represents a testcase, that is verified in isolation.

```
inputs/
bool/
double/
int32/
...
```

## Test case directory structure

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`.

```bash
bool/
bool.proto
bool.json
test_bool.py # optional
```

### proto

`<name>.proto` &mdash; *The protobuf message to test*

```protobuf
syntax = "proto3";
message Test {
bool value = 1;
}
```

You can add multiple `.proto` files to the test case, as long as one file matches the directory name.

### json

`<name>.json` &mdash; *Test-data to validate the message with*

```json
{
"value": true
}
```

### pytest

`test_<name>.py` &mdash; *Custom test to validate specific aspects of the generated class*

```python
from betterproto.tests.output_betterproto.bool.bool import Test

def test_value():
message = Test()
assert not message.value, "Boolean is False by default"
```

## Standard tests

The following tests are automatically executed for all cases:

- [x] Can the generated python code imported?
- [x] Can the generated message class be instantiated?
- [x] Is the generated code compatible with the Google's `grpc_tools.protoc` implementation?

## Running the tests

- `pipenv run generate`
This generates
- `betterproto/tests/output_betterproto` &mdash; *the plugin generated python classes*
- `betterproto/tests/output_reference` &mdash; *reference implementation classes*
- `pipenv run test`

2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[pytest]
python_files = test*.py
python_files = test_*.py
python_classes =
norecursedirs = **/output_*
addopts = -p no:warnings

0 comments on commit 77c0441

Please sign in to comment.