class WhenAnActionMethodIsNotSoAmbiguous:
    @classmethod
    def examples_of_not_so_ambiguous_action_names(self):
        def method_with_because_and_when_in_the_name():
            pass

        yield method_with_because_and_when_in_the_name

        def method_with_when_and_since_in_the_name(self):
            pass

        yield method_with_when_and_since_in_the_name

        def method_with_since_and_after_in_the_name(self):
            pass

        yield method_with_since_and_after_in_the_name

        def method_with_after_and_because_in_the_name(self):
            pass

        yield method_with_after_and_because_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_an_action(self):
        assert self.result is ACTION
Ejemplo n.º 2
0
class WhenASetupMethodIsNotSoAmbiguous:
    @classmethod
    def examples_of_not_so_ambiguous_setup_names(self):
        def method_with_establish_and_context_in_the_name():
            pass

        yield method_with_establish_and_context_in_the_name

        def method_with_context_and_given_in_the_name():
            pass

        yield method_with_context_and_given_in_the_name

        def method_with_given_and_establish_in_the_name():
            pass

        yield method_with_given_and_establish_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(
            self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_setup(self):
        assert self.result is SETUP
class WhenASetupMethodIsNotSoAmbiguous:
    @classmethod
    def examples_of_not_so_ambiguous_setup_names(self):
        def method_with_establish_and_context_in_the_name():
            pass

        yield method_with_establish_and_context_in_the_name

        def method_with_context_and_given_in_the_name():
            pass

        yield method_with_context_and_given_in_the_name

        def method_with_given_and_establish_in_the_name():
            pass

        yield method_with_given_and_establish_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_setup(self):
        assert self.result is SETUP
Ejemplo n.º 4
0
class WhenAnActionMethodIsNotSoAmbiguous:
    @classmethod
    def examples_of_not_so_ambiguous_action_names(self):
        def method_with_because_and_when_in_the_name():
            pass

        yield method_with_because_and_when_in_the_name

        def method_with_when_and_since_in_the_name(self):
            pass

        yield method_with_when_and_since_in_the_name

        def method_with_since_and_after_in_the_name(self):
            pass

        yield method_with_since_and_after_in_the_name

        def method_with_after_and_because_in_the_name(self):
            pass

        yield method_with_after_and_because_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(
            self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_an_action(self):
        assert self.result is ACTION
class WhenIdentifyingANonTestFolder:
    @classmethod
    def examples_of_non_test_folder_names(self):
        yield 'innocent_folder'
        yield os.path.join('tests', 'subfolder')
        yield os.path.join('spec', 'another_subfolder')

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_folder(self, folder):
        self.result = self.identifier.identify_folder(folder)

    def it_should_not_point_any_fingers(self):
        assert self.result is None
class WhenIdentifyingAFolder:
    @classmethod
    def examples_of_legal_test_folder_names(self):
        yield 'test'
        yield os.path.join('a_folder', 'tests')
        yield 'spec'
        yield os.path.join('another_folder', 'specs')

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_folder(self, folder):
        self.result = self.identifier.identify_folder(folder)

    def it_should_identify_it_as_a_test_folder(self):
        assert self.result is TEST_FOLDER
Ejemplo n.º 7
0
class WhenIdentifyingAFile:
    @classmethod
    def examples_of_legal_test_file_names(self):
        yield 'test.py'
        yield os.path.join('a_folder', 'tests.py')
        yield 'spec.py'
        yield os.path.join('another_folder', 'specs.py')

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_file(self, file):
        self.result = self.identifier.identify_file(file)

    def it_should_identify_it_as_a_test_file(self):
        assert self.result is TEST_FILE
class WhenAMethodContainsAKeywordAsPartOfAWord:
    @classmethod
    @examples
    def examples_of_methods_with_it_in_part_of_a_word(self):
        def itinerary(self):
            pass
        yield itinerary

    def given_an_identifier(self):
        self.identifier = NameBasedIdentifier()

    def when_i_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_ignore_it(self):
        assert self.result is None
class WhenIdentifyingATeardownMethod:
    @classmethod
    def examples_of_legal_teardown_names(self):
        def method_with_cleanup_in_the_name():
            pass

        yield method_with_cleanup_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_a_teardown(self):
        assert self.result is TEARDOWN
class WhenAnExamplesMethodIsNotSoAmbiguous:
    @classmethod
    def examples_of_not_so_ambiguous_examples_names(self):
        def method_with_example_and_data_in_the_name():
            pass
        yield method_with_example_and_data_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    @assertion
    def it_should_identify_it_as_examples(self):
        assert self.result is EXAMPLES
Ejemplo n.º 11
0
class WhenIdentifyingANonTestFolder:
    @classmethod
    def examples_of_non_test_folder_names(self):
        yield 'innocent_folder'
        yield os.path.join('tests', 'subfolder')
        yield os.path.join('spec', 'another_subfolder')

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_folder(
            self, folder):
        self.result = self.identifier.identify_folder(folder)

    def it_should_not_point_any_fingers(self):
        assert self.result is None
class WhenIdentifyingANonTestFile:
    @classmethod
    def examples_of_non_test_file_names(self):
        yield 'innocent_file'
        yield 'test_without_extension'
        yield 'test_with_other_extension.cs'
        yield os.path.join('tests', 'innocent_file.py')
        yield os.path.join('folder_with_spec_and.py', 'another_file.py')

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_file(self, file):
        self.result = self.identifier.identify_file(file)

    def it_should_not_point_any_fingers(self):
        assert self.result is None
Ejemplo n.º 13
0
class WhenAMethodContainsAKeywordAsPartOfAWord:
    @classmethod
    @examples
    def examples_of_methods_with_it_in_part_of_a_word(self):
        def itinerary(self):
            pass

        yield itinerary

    def given_an_identifier(self):
        self.identifier = NameBasedIdentifier()

    def when_i_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_ignore_it(self):
        assert self.result is None
Ejemplo n.º 14
0
class WhenIdentifyingATeardownMethod:
    @classmethod
    def examples_of_legal_teardown_names(self):
        def method_with_cleanup_in_the_name():
            pass

        yield method_with_cleanup_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(
            self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_a_teardown(self):
        assert self.result is TEARDOWN
Ejemplo n.º 15
0
class WhenIdentifyingANonTestFile:
    @classmethod
    def examples_of_non_test_file_names(self):
        yield 'innocent_file'
        yield 'test_without_extension'
        yield 'test_with_other_extension.cs'
        yield os.path.join('tests', 'innocent_file.py')
        yield os.path.join('folder_with_spec_and.py', 'another_file.py')

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_file(self, file):
        self.result = self.identifier.identify_file(file)

    def it_should_not_point_any_fingers(self):
        assert self.result is None
Ejemplo n.º 16
0
class WhenAnExamplesMethodIsNotSoAmbiguous:
    @classmethod
    def examples_of_not_so_ambiguous_examples_names(self):
        def method_with_example_and_data_in_the_name():
            pass

        yield method_with_example_and_data_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(
            self, method):
        self.result = self.identifier.identify_method(method)

    @assertion
    def it_should_identify_it_as_examples(self):
        assert self.result is EXAMPLES
class WhenIdentiyingANormalMethod:
    @classmethod
    def examples_of_uninteresting_names(self):
        def an_innocuous_function(self):
            pass

        yield an_innocuous_function

        def another_method(self):
            pass

        yield another_method

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_ignore_it(self):
        assert self.result is None
Ejemplo n.º 18
0
class WhenIdentifyingANormalClass:
    @classmethod
    def examples_of_legal_test_class_names(self):
        class ANormalClass:
            pass

        yield ANormalClass

        class AnotherNormalClass:
            pass

        yield AnotherNormalClass

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_class(self, cls):
        self.result = self.identifier.identify_class(cls)

    def it_should_ignore_it(self):
        assert self.result is None
class WhenIdentifyingANormalClass:
    @classmethod
    def examples_of_legal_test_class_names(self):
        class ANormalClass:
            pass

        yield ANormalClass

        class AnotherNormalClass:
            pass

        yield AnotherNormalClass

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_class(self, cls):
        self.result = self.identifier.identify_class(cls)

    def it_should_ignore_it(self):
        assert self.result is None
Ejemplo n.º 20
0
class WhenIdentifyingAClass:
    @classmethod
    def examples_of_legal_test_class_names(self):
        class ClassWithWhenInTheName:
            pass

        yield ClassWithWhenInTheName

        class ClassWithSpecInTheName:
            pass

        yield ClassWithSpecInTheName

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_class(self, cls):
        self.result = self.identifier.identify_class(cls)

    @assertion
    def it_should_identify_it_as_a_context(self):
        assert self.result is CONTEXT
class WhenIdentifyingAClass:
    @classmethod
    def examples_of_legal_test_class_names(self):
        class ClassWithWhenInTheName:
            pass

        yield ClassWithWhenInTheName

        class ClassWithSpecInTheName:
            pass

        yield ClassWithSpecInTheName

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_class(self, cls):
        self.result = self.identifier.identify_class(cls)

    @assertion
    def it_should_identify_it_as_a_context(self):
        assert self.result is CONTEXT
Ejemplo n.º 22
0
class WhenIdentiyingANormalMethod:
    @classmethod
    def examples_of_uninteresting_names(self):
        def an_innocuous_function(self):
            pass

        yield an_innocuous_function

        def another_method(self):
            pass

        yield another_method

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(
            self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_ignore_it(self):
        assert self.result is None
Ejemplo n.º 23
0
class WhenIdentifyingAnAssertionMethod:
    @classmethod
    def examples_of_legal_assertion_names(self):
        def method_with_it_in_the_name():
            pass

        yield method_with_it_in_the_name

        def method_with_should_in_the_name(self):
            pass

        yield method_with_should_in_the_name

        def method_with_then_in_the_name(self):
            pass

        yield method_with_then_in_the_name

        def method_with_must_in_the_name(self):
            pass

        yield method_with_must_in_the_name

        def method_with_will_in_the_name(self):
            pass

        yield method_with_will_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(
            self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_an_assertion(self):
        assert self.result is ASSERTION
class WhenIdentifyingAnAssertionMethod:
    @classmethod
    def examples_of_legal_assertion_names(self):
        def method_with_it_in_the_name():
            pass

        yield method_with_it_in_the_name

        def method_with_should_in_the_name(self):
            pass

        yield method_with_should_in_the_name

        def method_with_then_in_the_name(self):
            pass

        yield method_with_then_in_the_name

        def method_with_must_in_the_name(self):
            pass

        yield method_with_must_in_the_name

        def method_with_will_in_the_name(self):
            pass

        yield method_with_will_in_the_name

    def establish(self):
        self.identifier = NameBasedIdentifier()

    def because_the_framework_asks_the_plugin_to_identify_the_method(self, method):
        self.result = self.identifier.identify_method(method)

    def it_should_identify_it_as_an_assertion(self):
        assert self.result is ASSERTION
 def establish(self):
     self.identifier = NameBasedIdentifier()
Ejemplo n.º 26
0
 def given_an_identifier(self):
     self.identifier = NameBasedIdentifier()
 def given_an_identifier(self):
     self.identifier = NameBasedIdentifier()
Ejemplo n.º 28
0
 def establish(self):
     self.identifier = NameBasedIdentifier()
import inspect
import pathlib
import pytest
from contexts.core import (
    NO_EXAMPLE,
    Context,
    PluginComposite,
    TestClass,
    run_with_test_data,
)
from contexts.plugins.identification import NameBasedIdentifier
from contexts.plugins.identification.decorators import DecoratorBasedIdentifier

PLUGINS = PluginComposite([DecoratorBasedIdentifier(), NameBasedIdentifier()])


def pytest_pycollect_makeitem(collector, name, obj):
    if not inspect.isclass(obj) or not PLUGINS.identify_class(obj):
        return
    if hasattr(ContextsCollector, 'from_parent'):
        return ContextsCollector.from_parent(collector, name=name, obj=obj)
    return ContextsCollector(name, obj=obj, parent=collector)


class ContextsCollector(pytest.Collector):
    def __init__(self, name, obj, parent):
        self.name = name
        self.obj = obj
        self.path = inspect.getfile(self.obj)
        super().__init__(self.name, parent=parent)