Skip to content

Commit

Permalink
benchmark-base-case (#112)
Browse files Browse the repository at this point in the history
* benchmark-base-case

* up-version-to-0.2.10
  • Loading branch information
z00sts authored Mar 17, 2017
1 parent c742144 commit d68ae95
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 2 deletions.
1 change: 1 addition & 0 deletions contesto/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down
22 changes: 22 additions & 0 deletions contesto/basis/benchmark.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion contesto/basis/test_case.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 3 additions & 0 deletions contesto/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Config(object):
"collect_metadata": "",
"record_screencast": ""
}
benchmark = {
"run_count": ""
}
logging = {}

def __init__(self, *args):
Expand Down
3 changes: 3 additions & 0 deletions contesto/config/config.default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ platform: ANY
[browsermobproxy]
enabled: 0
url: localhost:8080

[benchmark]
run_count: 1
32 changes: 32 additions & 0 deletions examples/benchmark_example.py
Original file line number Diff line number Diff line change
@@ -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\'')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 40 additions & 0 deletions tests/benchmark_tests.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d68ae95

Please sign in to comment.