diff --git a/contesto/__init__.py b/contesto/__init__.py index d064dc3..1dcda00 100644 --- a/contesto/__init__.py +++ b/contesto/__init__.py @@ -1,6 +1,7 @@ from contesto.config import config from contesto.basis.test_case import ContestoTestCase +from contesto.basis.benchmark import BenchmarkBaseCase from contesto.basis.page import Page, WebPage, MobilePage from contesto.basis.component import Component, WebComponent, MobileComponent from contesto.core.locator import * diff --git a/contesto/basis/benchmark.py b/contesto/basis/benchmark.py new file mode 100644 index 0000000..3908150 --- /dev/null +++ b/contesto/basis/benchmark.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +from contesto import ContestoTestCase, config +from contesto.utils.log import log + + +class BenchmarkBaseCase(ContestoTestCase): + def __init__(self, test_name='runTest'): + super(BenchmarkBaseCase, self).__init__(test_name) + self.run_count = config.benchmark.get('run_count', 1) + self._single_method = getattr(self, self._testMethodName) + setattr(self, self._testMethodName, self.run_multiple_times) + + def run_multiple_times(self): + for i in range(1, self.run_count + 1): + log.debug('Benchmark iter {}'.format(i)) + try: + res = self._single_method() + except: + log.exception('Benchmark iter {} exception:'.format(i)) + raise + return res diff --git a/contesto/basis/test_case.py b/contesto/basis/test_case.py index 6a5c2dc..bc60030 100644 --- a/contesto/basis/test_case.py +++ b/contesto/basis/test_case.py @@ -1,7 +1,7 @@ # coding: utf-8 from functools import wraps from time import sleep -from types import MethodType + try: from urllib2 import URLError except ImportError: diff --git a/contesto/config/__init__.py b/contesto/config/__init__.py index fe206a1..46fed56 100644 --- a/contesto/config/__init__.py +++ b/contesto/config/__init__.py @@ -33,6 +33,9 @@ class Config(object): "collect_metadata": "", "record_screencast": "" } + benchmark = { + "run_count": "" + } logging = {} def __init__(self, *args): diff --git a/contesto/config/config.default.ini b/contesto/config/config.default.ini index 0f081b6..0f4b0a6 100644 --- a/contesto/config/config.default.ini +++ b/contesto/config/config.default.ini @@ -8,3 +8,6 @@ platform: ANY [browsermobproxy] enabled: 0 url: localhost:8080 + +[benchmark] +run_count: 1 diff --git a/examples/benchmark_example.py b/examples/benchmark_example.py new file mode 100644 index 0000000..35fd136 --- /dev/null +++ b/examples/benchmark_example.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- + +from contesto import BenchmarkBaseCase + + +class TestBenchmarkExamples(BenchmarkBaseCase): + @classmethod + def setUpClass(cls): + print('\nmake setupclass') + + @classmethod + def tearDownClass(cls): + print('make teardownclass') + + def setUp(self): + print('\tmake setup') + + def tearDown(self): + print('\tmake teardown') + + def test_benchmark_app_start(self): + """ + AppStart scenario example ("do nothing") + """ + print('\t\tHey! I\'m app-start scenario!') + + def test_benchmark_search(self): + """ + Search scenario example + """ + print('\t\tHey! I\'m search scenario!') + print('\t\tSearching \'beer\'') diff --git a/setup.py b/setup.py index f1ad6d1..b41c95f 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ 'config/*.ini' ], }, - "version": "0.2.9", + "version": "0.2.10", "install_requires": [ "selenium==2.52.0", "Appium-Python-Client==0.24", diff --git a/tests/benchmark_tests.py b/tests/benchmark_tests.py new file mode 100644 index 0000000..47aa772 --- /dev/null +++ b/tests/benchmark_tests.py @@ -0,0 +1,40 @@ +# coding: utf-8 + +try: + from mock import Mock +except ImportError: + from unittest.mock import Mock + +import unittest + +from contesto import BenchmarkBaseCase, config + + +def make(): + class TestFakeBenchmark(BenchmarkBaseCase): + mock = Mock() + + @classmethod + def _create_session(cls, *args, **kwargs): + return Mock() + + def test_method(self): + self.mock() + + return TestFakeBenchmark('test_method') + + +class TestBenchmarkBaseCase(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls._config_benchmark_run_count = config.benchmark["run_count"] + config.benchmark["run_count"] = 5 + + @classmethod + def tearDownClass(cls): + config.benchmark["run_count"] = cls._config_benchmark_run_count + + def test_multiple_run(self): + benchmark = make() + benchmark.run() + self.assertEqual(benchmark.mock.call_count, 5)