Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest related changes #248

Merged
merged 113 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
4f244d3
Adapt project to run tests with pytest.
sstrehlk May 17, 2021
4094609
Update requirements.txt
sstrehlk May 19, 2021
cfd9e93
updated tests marking
sstrehlk May 19, 2021
fbe66bd
Update conftest.py
sstrehlk May 19, 2021
daca7b8
Update conftest.py
sstrehlk May 19, 2021
2f13f6e
Added test_marking manual.
sstrehlk May 20, 2021
f80eea5
Cosmetic corrections in test_marking.md
sstrehlk May 20, 2021
aab6bd7
adapting cli/test_yolo_format.py for pytest
sstrehlk May 20, 2021
45e9470
Updated test_yolo_format.py
sstrehlk May 21, 2021
46e1fe7
Corrected datumaro_components.py, updated license in added files.
sstrehlk May 21, 2021
bd8a335
Updated test_marking.md file
sstrehlk May 21, 2021
f0f343b
Updated test_marking.md file
sstrehlk May 21, 2021
7df661c
Updates after review.
sstrehlk May 24, 2021
ea804b1
Removed unnecessary marking.
sstrehlk May 24, 2021
7cc26a6
removed unneeded comments in requirements.py
sstrehlk May 24, 2021
e7d845a
Corrected requirement constant.
sstrehlk May 24, 2021
299cc26
experiment related to test_can_import test.
sstrehlk May 25, 2021
60ace83
Corrected accidentally changed indentation.
sstrehlk May 25, 2021
cfeb7d9
Updated .travis.yml to work with pytest
sstrehlk May 26, 2021
e98f5e3
Updated .travis.yml to work with pytest.
sstrehlk May 26, 2021
fe8e510
Updated .gitignore, .travis.yml
sstrehlk May 26, 2021
f0fe579
Updated .travis.yml to check pytest performance in Travis.
sstrehlk May 26, 2021
9a164ce
Updated .travis.yml and requirements.txt.
sstrehlk May 26, 2021
bb3dd8a
Merged two imports into one line in test files.
sstrehlk May 27, 2021
38a9abd
Removed unneded __test__ variable.
sstrehlk May 27, 2021
9b30e6f
Corrections in names and formatting.
sstrehlk May 27, 2021
5bbb754
Implemented and used mark_requirement decorator
sstrehlk May 28, 2021
1c72c0a
Adapt project to run tests with pytest.
sstrehlk May 17, 2021
84dd16b
Update requirements.txt
sstrehlk May 19, 2021
b66fd2b
updated tests marking
sstrehlk May 19, 2021
1b38dc5
Update conftest.py
sstrehlk May 19, 2021
a02f956
Update conftest.py
sstrehlk May 19, 2021
f1ab1cf
Added test_marking manual.
sstrehlk May 20, 2021
c103080
Cosmetic corrections in test_marking.md
sstrehlk May 20, 2021
ac40b93
adapting cli/test_yolo_format.py for pytest
sstrehlk May 20, 2021
dc0df85
Updated test_yolo_format.py
sstrehlk May 21, 2021
0867e02
Corrected datumaro_components.py, updated license in added files.
sstrehlk May 21, 2021
514ab9f
Updated test_marking.md file
sstrehlk May 21, 2021
4da26ad
Updated test_marking.md file
sstrehlk May 21, 2021
854d052
Updates after review.
sstrehlk May 24, 2021
9824f90
Removed unnecessary marking.
sstrehlk May 24, 2021
fbc449c
removed unneeded comments in requirements.py
sstrehlk May 24, 2021
019cd16
Corrected requirement constant.
sstrehlk May 24, 2021
15eee86
experiment related to test_can_import test.
sstrehlk May 25, 2021
a29d297
Corrected accidentally changed indentation.
sstrehlk May 25, 2021
ce37288
Updated .travis.yml to work with pytest
sstrehlk May 26, 2021
407e029
Updated .travis.yml to work with pytest.
sstrehlk May 26, 2021
5599849
Updated .gitignore, .travis.yml
sstrehlk May 26, 2021
9621fdd
Updated .travis.yml to check pytest performance in Travis.
sstrehlk May 26, 2021
8d37efb
Updated .travis.yml and requirements.txt.
sstrehlk May 26, 2021
d61169f
Merged two imports into one line in test files.
sstrehlk May 27, 2021
be1ea4a
Removed unneded __test__ variable.
sstrehlk May 27, 2021
9269822
Corrections in names and formatting.
sstrehlk May 27, 2021
9f8abc2
Implemented and used mark_requirement decorator
sstrehlk May 28, 2021
1246129
Merge branch 'pytest_related_changes' of https://github.com/openvinot…
sstrehlk May 31, 2021
64a9115
Reverted change related to pycocotools version. It's discussed in Iss…
sstrehlk May 31, 2021
3f431ed
Updated CHANGELOG.md
sstrehlk May 31, 2021
c7e74da
Adapt project to run tests with pytest.
sstrehlk May 17, 2021
ee1e8a9
Update requirements.txt
sstrehlk May 19, 2021
3af4a2d
updated tests marking
sstrehlk May 19, 2021
2ae2efa
Update conftest.py
sstrehlk May 19, 2021
3245701
Update conftest.py
sstrehlk May 19, 2021
1c9b289
Added test_marking manual.
sstrehlk May 20, 2021
2bee3b5
Cosmetic corrections in test_marking.md
sstrehlk May 20, 2021
18182d6
adapting cli/test_yolo_format.py for pytest
sstrehlk May 20, 2021
d0389dc
Updated test_yolo_format.py
sstrehlk May 21, 2021
44e9e70
Corrected datumaro_components.py, updated license in added files.
sstrehlk May 21, 2021
9ad6a6e
Updated test_marking.md file
sstrehlk May 21, 2021
dd7e2eb
Updated test_marking.md file
sstrehlk May 21, 2021
9307181
Updates after review.
sstrehlk May 24, 2021
5a41f60
Removed unnecessary marking.
sstrehlk May 24, 2021
6f8ac20
removed unneeded comments in requirements.py
sstrehlk May 24, 2021
e61e97f
Corrected requirement constant.
sstrehlk May 24, 2021
6474be2
experiment related to test_can_import test.
sstrehlk May 25, 2021
4025c62
Corrected accidentally changed indentation.
sstrehlk May 25, 2021
f8ce252
Updated .travis.yml to work with pytest
sstrehlk May 26, 2021
3867d51
Updated .travis.yml to work with pytest.
sstrehlk May 26, 2021
1be8b3c
Updated .gitignore, .travis.yml
sstrehlk May 26, 2021
d390b4b
Updated .travis.yml to check pytest performance in Travis.
sstrehlk May 26, 2021
c2f0f64
Updated .travis.yml and requirements.txt.
sstrehlk May 26, 2021
d8c518d
Merged two imports into one line in test files.
sstrehlk May 27, 2021
1771bb9
Removed unneded __test__ variable.
sstrehlk May 27, 2021
a9144cd
Corrections in names and formatting.
sstrehlk May 27, 2021
cc63c5d
Implemented and used mark_requirement decorator
sstrehlk May 28, 2021
1ee44ee
Adapt project to run tests with pytest.
sstrehlk May 17, 2021
496ea03
Added test_marking manual.
sstrehlk May 20, 2021
2e79c48
Cosmetic corrections in test_marking.md
sstrehlk May 20, 2021
dfd8a91
adapting cli/test_yolo_format.py for pytest
sstrehlk May 20, 2021
68ab086
Corrected datumaro_components.py, updated license in added files.
sstrehlk May 21, 2021
15980a9
Updated test_marking.md file
sstrehlk May 21, 2021
e71cd8c
Updated test_marking.md file
sstrehlk May 21, 2021
6127c62
Updates after review.
sstrehlk May 24, 2021
96c4c4a
removed unneeded comments in requirements.py
sstrehlk May 24, 2021
1e40654
Corrected accidentally changed indentation.
sstrehlk May 25, 2021
2772c04
Updated .travis.yml to work with pytest.
sstrehlk May 26, 2021
91b58bb
Updated .gitignore, .travis.yml
sstrehlk May 26, 2021
3cce32d
Updated .travis.yml and requirements.txt.
sstrehlk May 26, 2021
0b4758a
Merged two imports into one line in test files.
sstrehlk May 27, 2021
9c06cc2
Removed unneded __test__ variable.
sstrehlk May 27, 2021
a5feca0
Reverted change related to pycocotools version. It's discussed in Iss…
sstrehlk May 31, 2021
90cb339
Updated CHANGELOG.md
sstrehlk May 31, 2021
ced967c
Merge branch 'pytest_related_changes' of https://github.com/openvinot…
sstrehlk Jun 1, 2021
83204ce
github testing scripts adapted to pytest
sstrehlk Jun 1, 2021
f856e47
Corrected rebase.
sstrehlk Jun 1, 2021
6f8328f
Merge branch 'pytest_related_changes' of https://github.com/openvinot…
sstrehlk Jun 1, 2021
83cd7c2
Merge branch 'develop' into pytest_related_changes
Jun 1, 2021
84eb6f7
update changelog
Jun 1, 2021
3fa8112
Remove extra comments
Jun 1, 2021
2158ebe
Move test descriptions to contributing guide
Jun 1, 2021
b95013a
Update imports
Jun 1, 2021
5ad70cf
Update test annotations
Jun 1, 2021
a0faa20
Update markings
Jun 1, 2021
e32153a
Added component test run type marking for datumaro tests.
sstrehlk Jun 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ coverage.xml
cover/

# Sphinx documentation
docs/_build/
docs/_build/

#Pycharm config files
.idea/
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ matrix:
- dist: bionic
python: '3.6'
before_install:
- pip install coverage
- pip install pytest-cov
script:
- coverage run -m unittest discover -v
- coverage run -a datum.py -h
- pytest -v --cov --cov-report xml:coverage.xml
- datum -h
after_success:
- coverage xml
- bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage.xml

- dist: bionic
Expand All @@ -34,5 +33,5 @@ install:
- pip install pandas

script:
- python -m unittest discover -v
- pytest -v
- datum -h
2 changes: 2 additions & 0 deletions datumaro/components/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ def get(self, id, subset=None) -> Optional[DatasetItem]:
raise NotImplementedError()

class Extractor(IExtractor):
__test__ = False

def __init__(self, length=None, subsets=None):
self._length = length
self._subsets = subsets
Expand Down
4 changes: 3 additions & 1 deletion datumaro/util/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class TestDir(FileRemover):
...
"""

__test__ = False

def __init__(self, path=None):
if path is None:
path = osp.abspath('temp_%s-' % current_function_name(2))
Expand Down Expand Up @@ -143,7 +145,7 @@ def compare_datasets_strict(test, expected, actual):
'%s:\n%s\nvs.\n%s\n' % \
(idx, item_a, item_b))

def test_save_and_load(test, source_dataset, converter, test_dir, importer,
def check_save_and_load(test, source_dataset, converter, test_dir, importer,
target_dataset=None, importer_args=None, compare=None, **kwargs):
converter(source_dataset, test_dir)

Expand Down
182 changes: 182 additions & 0 deletions docs/test_marking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# Source code elements related to pytest
## Contents
- [Test case description](#Test_case_description)
- [Test marking](#Test_marking)
- [Requirements](#Requirements)
- [Type](#Type)
- [Priority](#Priority)
- [Component](#Component)
- [Skip](#Skip)
- [Bug(s)](#Bug)
- [Parameters](#Parameters)
- [Fixtures](#Fixtures)
- [Test naming convention](#TestNaming)
- [Test name](#TestName)
- [Test documentation](#TestDoc)
- [Docstring](#Docstring)
- [Steps in test body](#Step)

<a id="Test_case_description"></a>
## Test case description
<a id="Test_marking"></a>
### Test marking
<a id="Requirements"></a>
#### Requirements
Fully defined in GitHub issues, e.g.
```
@pytest.mark.reqids(Requirements.DATUM_244, Requirements.DATUM_333)
```
Requirements constants need to be added to datumaro/tests/constants/requirements.py for example:
```
# [DATUMARO] Add SNYK scan integration
DATUM_244 = "DATUM-244 Add Snyk integration"
```
Recommended notation for requirements

```python
@pytest.mark.requids(Requirements.DATUM_123)
```
where DATUM is a keyword for datumaro indication, and number is a datumaro github issue number.

<a id="Type"></a>
#### Type
Test types: gui_smoke, gui_regression, manual, gui_other, gui_long, api, component, unit e.g.
```python
@pytest.mark.component
@pytest.mark.unit
```

<a id="Priority"></a>
#### Priority
Test priorities: low, medium, high, e.g.,
```python
@pytest.mark.priority_low
@pytest.mark.priority_medium
@pytest.mark.priority_high
```
<a id="Component"></a>
#### Component

Component marking used for indication of different system components e.g.
```python
@pytest.mark.components(DatumaroComponent.Datumaro)
```
<a id="Skip"></a>
#### Skip

For marking tests, which should be skipped for example for not yet tests, e.g.,
```python
@pytest.mark.skip(reason=SkipMessages.NOT_IMPLEMENTED)
```
<a id="Bug"></a>
#### Bug(s):

In case of test failure, bug should be entered into github issues, and test can be marked e.g.
```python
@pytest.mark.bugs("DATUM-219 - Return format is not uniform")
```
<a id="Parameters"></a>
#### Parameters:

Parameters are used for running the same test with different parameters e.g.
```python
@pytest.mark.parametrize("numpy_array, batch_size", [
(np.zeros([2]), 0),
(np.zeros([2]), 1),
(np.zeros([2]), 2),
(np.zeros([2]), 5),
(np.zeros([5]), 2),
])
```

<a id="Fixtures"></a>
#### Fixtures

If needed pytest fixtures can be used - for more details see pytest documentation <br>
https://docs.pytest.org/en/6.2.x/contents.html <br>
(be aware that fixtures are supported in pytest, and they are not supported in unittests)

<a id="TestNaming"></a>
### Test naming convention

<a id="TestName"></a>
#### Test name:

Test method should be prefixed with "test_" prefix e.g.
```python
test_*()
```
"test_" prefix is an indication for pytest to treat method as a test for execution.
It's important not to start other methods with the "test_" prefix.

<a id="DestDoc"></a>
### Test documentation

<a id="Docstring"></a>
#### Docstring

Tests are documented with Docstring. Every test method documentation string should contain: Description, Expected results
and Steps. These fields are required but, not limited e.g.
```python
def test_can_convert_polygons_to_mask(self):
"""
<b>Description:</b>
Ensure that the dataset polygon annotation can be properly converted into dataset segmentation mask.

<b>Expected results:</b>
Dataset segmentation mask converted from dataset polygon annotation is equal to expected mask.

<b>Steps:</b>
1. Prepare dataset with polygon annotation (source dataset)
2. Prepare dataset with expected mask segmentation mode (target dataset)
3. Convert source dataset to target, with conversion of annotation from polygon to mask. Verify that result
segmentation mask is equal to expected mask.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it can quickly become a story similar to the story with comments in code. If a test is so complex it is impossible to understand quickly, it must be split and simplified. For many unit tests such description will be equal, or even bigger that the test code itself. It might be ok for complex or long integration/e2e tests, which are supposed to be like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This high level description is dedicated to reports viewers. Viewer don't need to know particular programming language to have understanding what was tested and how.


"""

```
<a id="Steps"></a>
#### Steps in test body:

Steps description in test body are placed as a code comment lines e.g.
```python
# 1. Prepare dataset with polygon annotation (source dataset)
source_dataset = Dataset.from_iterable([
DatasetItem(id=1, image=np.zeros((6, 10, 3)),
annotations=[
Polygon([0, 0, 4, 0, 4, 4],
label=3, id=4, group=4),
Polygon([5, 0, 9, 0, 5, 5],
label=3, id=4, group=4),
]
),
], categories=[str(i) for i in range(10)])

# 2. Prepare dataset with expected mask segmentation mode (target dataset)
target_dataset = Dataset.from_iterable([
DatasetItem(id=1, image=np.zeros((6, 10, 3)),
annotations=[
Mask(np.array([
[0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
# only internal fragment (without the border),
# but not everywhere...
),
attributes={ 'is_crowd': True },
label=3, id=4, group=4),
], attributes={'id': 1}
),
], categories=[str(i) for i in range(10)])

# 3. Convert source dataset to target, with conversion of annotation from polygon to mask. Verify that result
# segmentation mask is equal to expected mask.
with TestDir() as test_dir:
self._test_save_and_load(source_dataset,
partial(CocoInstancesConverter.convert, segmentation_mode='mask'),
test_dir, target_dataset=target_dataset)

```
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ lxml>=4.4.1
matplotlib>=3.3.1
opencv-python-headless>=4.1.0.25
Pillow>=6.1.0
pycocotools>=2.0.0
pycocotools==2.0.0
zhiltsov-max marked this conversation as resolved.
Show resolved Hide resolved
PyYAML>=5.3.1
scikit-image>=0.15.0
tensorboardX>=1.8
zhiltsov-max marked this conversation as resolved.
Show resolved Hide resolved
pandas>=1.1.5
pytest>=5.3.5
8 changes: 8 additions & 0 deletions tests/cli/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@
from datumaro.util.image import Image
from datumaro.util.test_utils import TestDir

import pytest
from tests.requirements import Requirements
from tests.requirements import DatumaroComponent
zhiltsov-max marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.components(DatumaroComponent.Datumaro)
class DiffTest(TestCase):
@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_can_compare_projects(self): # just a smoke test
label_categories1 = LabelCategories.from_iterable(['x', 'a', 'b', 'y'])
mask_categories1 = MaskCategories.make_default(len(label_categories1))
Expand Down
27 changes: 27 additions & 0 deletions tests/cli/test_voc_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
from datumaro.cli.__main__ import main
from datumaro.util.test_utils import TestDir, compare_datasets

import pytest
from tests.requirements import Requirements
from tests.requirements import DatumaroComponent

DUMMY_DATASETS_DIR = osp.join(__file__[:__file__.rfind(osp.join('tests', ''))],
'tests', 'assets', 'voc_dataset')

def run(test, *args, expected_code=0):
test.assertEqual(expected_code, main(args), str(args))


@pytest.mark.components(DatumaroComponent.Datumaro)
class VocIntegrationScenarios(TestCase):
def _test_can_save_and_load(self, project_path, source_path, source_dataset,
dataset_format, result_path=None, label_map=None):
Expand All @@ -30,6 +36,9 @@ def _test_can_save_and_load(self, project_path, source_path, source_dataset,
target_dataset = Dataset.import_from(result_path, dataset_format)
compare_datasets(self, source_dataset, target_dataset)

@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_preparing_dataset_for_train_model(self):
source_dataset = Dataset.from_iterable([
DatasetItem(id='c', subset='train',
Expand Down Expand Up @@ -80,6 +89,9 @@ def test_preparing_dataset_for_train_model(self):
parsed_dataset = Dataset.import_from(export_path, format='voc')
compare_datasets(self, source_dataset, parsed_dataset)

@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_convert_to_voc_format(self):
label_map = OrderedDict(('label_' + str(i), [None, [], []]) for i in range(10))
label_map['background'] = [None, [], []]
Expand Down Expand Up @@ -122,6 +134,9 @@ def test_convert_to_voc_format(self):
parsed_dataset = Dataset.import_from(voc_export, format='voc')
compare_datasets(self, source_dataset, parsed_dataset)

@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_can_save_and_load_voc_dataset(self):
source_dataset = Dataset.from_iterable([
DatasetItem(id='2007_000001', subset='train',
Expand Down Expand Up @@ -164,6 +179,9 @@ def test_can_save_and_load_voc_dataset(self):
self._test_can_save_and_load(test_dir, voc_dir, source_dataset,
'voc', label_map='voc')

@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_can_save_and_load_voc_layout_dataset(self):
source_dataset = Dataset.from_iterable([
DatasetItem(id='2007_000001', subset='train',
Expand Down Expand Up @@ -196,6 +214,9 @@ def test_can_save_and_load_voc_layout_dataset(self):
self._test_can_save_and_load(test_dir, voc_layout_path, source_dataset,
'voc_layout', result_path=result_voc_path, label_map='voc')

@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_can_save_and_load_voc_detect_dataset(self):
source_dataset = Dataset.from_iterable([
DatasetItem(id='2007_000001', subset='train',
Expand Down Expand Up @@ -234,6 +255,9 @@ def test_can_save_and_load_voc_detect_dataset(self):
self._test_can_save_and_load(test_dir, voc_detection_path, source_dataset,
'voc_detection', result_path=result_voc_path, label_map='voc')

@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_can_save_and_load_voc_segmentation_dataset(self):
source_dataset = Dataset.from_iterable([
DatasetItem(id='2007_000001', subset='train',
Expand All @@ -252,6 +276,9 @@ def test_can_save_and_load_voc_segmentation_dataset(self):
self._test_can_save_and_load(test_dir, voc_segm_path, source_dataset,
'voc_segmentation', result_path=result_voc_path, label_map='voc')

@pytest.mark.priority_medium
@pytest.mark.reqids(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.component
def test_can_save_and_load_voc_action_dataset(self):
source_dataset = Dataset.from_iterable([
DatasetItem(id='2007_000001', subset='train',
Expand Down
Loading