Skip to content

Commit

Permalink
Read decorators and add them as new token
Browse files Browse the repository at this point in the history
Add support for reading and printing decorators in `testimony print`. They are not involved in validation.
  • Loading branch information
mirekdlugosz authored and elyezer committed Aug 2, 2019
1 parent 4929ea3 commit 5f70500
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 32 deletions.
27 changes: 27 additions & 0 deletions testimony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def __init__(self, function_def, parent_class=None, testmodule=None):
if value.required] or None
self.parser = DocstringParser(tokens, minimum_tokens)
self._parse_docstring()
self._parse_decorators()

def _parse_docstring(self):
"""Parse module, class and function docstrings.
Expand Down Expand Up @@ -160,6 +161,30 @@ def _parse_docstring(self):
docstring = self.docstring.decode('utf-8')
self.tokens['test'] = docstring.strip().split('\n')[0]

def _parse_decorators(self):
"""Get decorators from class and function definition.
Modules and packages can't be decorated, so they are skipped.
Decorator can be pytest marker or function call.
``tokens`` attribute will be updated with new value ``decorators``.
"""
token_decorators = []
for level in (self.parent_class_def, self.function_def):
decorators = getattr(level, 'decorator_list', None)
if not decorators:
continue

for decorator in decorators:
try:
token_decorators.append(
getattr(decorator, 'func', decorator).id
)
except AttributeError:
continue

if token_decorators:
self.tokens['decorators'] = token_decorators

@property
def has_valid_docstring(self):
"""Indicate if the docstring has the minimum tokens."""
Expand Down Expand Up @@ -224,6 +249,8 @@ def __str__(self):

output = []
for token, value in sorted(self.tokens.items()):
if isinstance(value, list):
value = ','.join(value)
output.append('{0}:\n{1}\n'.format(
token.capitalize(), indent(value, ' ')))
if self.invalid_tokens:
Expand Down
157 changes: 125 additions & 32 deletions tests/sample_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,98 @@ Test:
Unexpected tokens:
Caseautomation: AUTOMATED

DecoratorsTestCase::test_no_decorator:247
-----------------------------------------

Assert:
Decorators are properly read

Feature:
Decorators

Setup:
Global setup

Test:
Test without decorator.


DecoratorsTestCase::test_one_decorator:251
------------------------------------------

Assert:
Decorators are properly read

Decorators:
tier1

Feature:
Decorators

Setup:
Global setup

Test:
Test with one decorator.


DecoratorsTestCase::test_multiple_decorators:256
------------------------------------------------

Assert:
Decorators are properly read

Decorators:
tier1,tier2

Feature:
Decorators

Setup:
Global setup

Test:
Test with multiple decorators.


MergeDecoratorsTestCase::test_no_decorator:271
----------------------------------------------

Assert:
Decorators are properly read

Decorators:
tier1

Feature:
Decorators

Setup:
Global setup

Test:
Test without decorator.


MergeDecoratorsTestCase::test_decorator:275
-------------------------------------------

Assert:
Decorators are properly read

Decorators:
tier1,tier2

Feature:
Decorators

Setup:
Global setup

Test:
Test with one decorator.


tests/sample_pkg/test_sample2.py
================================

Expand Down Expand Up @@ -325,17 +417,18 @@ Test:
= summary report =
==================

Total number of tests: 16
Test cases with no docstrings: 1 (06.25%)
Assert: 14 (87.50%)
Bz: 2 (12.50%)
Feature: 13 (81.25%)
Setup: 15 (93.75%)
Status: 7 (43.75%)
Steps: 8 (50.00%)
Tags: 5 (31.25%)
Test: 15 (93.75%)
Type: 1 (06.25%)
Total number of tests: 21
Test cases with no docstrings: 1 (04.76%)
Assert: 19 (90.48%)
Bz: 2 (09.52%)
Decorators: 4 (19.05%)
Feature: 18 (85.71%)
Setup: 20 (95.24%)
Status: 7 (33.33%)
Steps: 8 (38.10%)
Tags: 5 (23.81%)
Test: 20 (95.24%)
Type: 1 (04.76%)

=============================
= validate_docstring report =
Expand Down Expand Up @@ -391,13 +484,13 @@ ConfigurationFileTestCase::test_case_mismatch_case_sensitive_values:226
* Unexpected tokens:
Caseautomation: AUTOMATED

Total number of tests: 16
Total number of invalid docstrings: 6 (37.50%)
Test cases with no docstrings: 1 (6.25%)
Test cases missing minimal docstrings: 3 (18.75%)
Test cases with unexpected tags: 3 (18.75%)
Total number of tests: 21
Total number of invalid docstrings: 6 (28.57%)
Test cases with no docstrings: 1 (4.76%)
Test cases missing minimal docstrings: 3 (14.29%)
Test cases with unexpected tags: 3 (14.29%)
Test cases with unexpected token values in docstrings: 0 (0.00%)
Test cases with unparseable docstrings: 1 (6.25%)
Test cases with unparseable docstrings: 1 (4.76%)

=========================================
= validate_docstring with config report =
Expand Down Expand Up @@ -454,38 +547,38 @@ ConfigurationFileTestCase::test_case_mismatch_case_sensitive_values:226
* Tokens with invalid values:
Caseautomation: AUTOMATED (type: 'choice')

Total number of tests: 16
Total number of invalid docstrings: 6 (37.50%)
Test cases with no docstrings: 1 (6.25%)
Test cases missing minimal docstrings: 3 (18.75%)
Test cases with unexpected tags: 1 (6.25%)
Test cases with unexpected token values in docstrings: 2 (12.50%)
Test cases with unparseable docstrings: 1 (6.25%)
Total number of tests: 21
Total number of invalid docstrings: 6 (28.57%)
Test cases with no docstrings: 1 (4.76%)
Test cases missing minimal docstrings: 3 (14.29%)
Test cases with unexpected tags: 1 (4.76%)
Test cases with unexpected token values in docstrings: 2 (9.52%)
Test cases with unparseable docstrings: 1 (4.76%)

====================================
= config should overwrite defaults =
====================================

default summary report length: 11
--config summary report length: 5
default summary report length: 12
--config summary report length: 6

====================================
= tokens should overwrite defaults =
====================================

default summary report length: 11
--tokens summary report length: 6
default summary report length: 12
--tokens summary report length: 7

============================================
= minimum-tokens should overwrite defaults =
============================================

default summary report length: 11
--minimum-tokens summary report length: 3
default summary report length: 12
--minimum-tokens summary report length: 4

==========================================================
= tokens and minimum-tokens should be merged with config =
==========================================================

default summary report length: 11
--tokens --minimum-tokens --config summary report length: 7
default summary report length: 12
--tokens --minimum-tokens --config summary report length: 8
41 changes: 41 additions & 0 deletions tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,44 @@ def test_case_mismatch_case_sensitive_values(self):
:CaseAutomation: AUTOMATED
"""
pass

class DecoratorsTestCase():
"""Class to test Testimony support for decorators
:Feature: Decorators
:Assert: Decorators are properly read
"""

def test_no_decorator(self):
"""Test without decorator."""
pass

@tier1
def test_one_decorator(self):
"""Test with one decorator."""
pass

@tier1
@tier2
def test_multiple_decorators(self):
"""Test with multiple decorators."""
pass

@tier1
class MergeDecoratorsTestCase():
"""Class to test Testimony support for decorated classes
:Feature: Decorators
:Assert: Decorators are properly read
"""

def test_no_decorator(self):
"""Test without decorator."""
pass

@tier2
def test_decorator(self):
"""Test with one decorator."""
pass

0 comments on commit 5f70500

Please sign in to comment.