Last active
April 11, 2019 13:56
-
-
Save eilst/cb5cb0ae88cc508b8d6a82854a190e09 to your computer and use it in GitHub Desktop.
Script for executing a single test case in odoo 11 (depends on anybox.recipe.odoo.runtime.session)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys | |
import os | |
import inspect | |
import ipdb | |
sys.path.append('/opt/odoodev/git/odoo') | |
sys.path.append('/opt/odoodev/git/tools/src/anybox.recipe.odoo') | |
from anybox.recipe.odoo.runtime.session import Session | |
import odoo | |
if len(sys.argv) <= 3: | |
print('\nScript for executing a single test case in odoo\n') | |
print('odoo-single-test takes 3 arguments: \n') | |
print('Usage: odoo-single-test DATABASE_NAME TEST_FILE TEST_NAME \n') | |
print(' DATABASE_NAME: An existing database') | |
print(' TEST_FILE: The name of the file containing the test, it must be on the current working directory') | |
print(' TEST_NAME: The name of test to be executed \n') | |
print(' if "test" keyword is in place of TEST_NAME \n') | |
print(' all test in TEST_FILE will be executed \n') | |
exit() | |
session = Session('/opt/odoodev/git/tools/etc/dev.cfg','/opt/odoodev/git/tools') | |
cwd = os.getcwd() | |
sys.path.append(cwd) | |
def my_import(name): | |
components = name.split('.') | |
mod = __import__(components[0]) | |
for comp in components[1:]: | |
mod = getattr(mod, comp) | |
return mod | |
def get_method(self, name): | |
method = getattr(self,name) | |
return method | |
session.open(sys.argv[1]) | |
test_file = sys.argv[2] | |
test_method = sys.argv[3] | |
main_class = [m[0] for m in inspect.getmembers(__import__(test_file),inspect.isclass) if m[1].__module__ == test_file][0] | |
klass = my_import(test_file + '.' + main_class) | |
test = klass() | |
print("\nSetting up test...\U0001F913 \n") | |
test.setUp() | |
if test_method == 'test': | |
lst = [method_name for method_name in dir(test) | |
if callable(getattr(test, method_name))] | |
test_methods = [k for k in lst if 'test' in k] | |
total = len(test_methods) | |
error = 0 | |
error_list = [] | |
for t in test_methods: | |
method = get_method(test, t) | |
try: | |
print(" Test: " + t) | |
print(" testing... \n") | |
method() | |
print(" \U00002714\n") | |
print("\U0001F60E \U0001F60E \U0001F60E Success!!\U0001F60E \U0001F60E \U0001F60E\n") | |
print("===========================") | |
except Exception as e: | |
error += 1 | |
error_list.append(t) | |
print(" \U0000274C\n") | |
print("\U0001F62D \U0001F62D \U0001F62D Error\U0001F62D \U0001F62D \U0001F62D\n") | |
print(e) | |
print("===========================") | |
print("Success rate: " + str((total - error) / total) ) | |
print("Fails: " + str(error_list) ) | |
else: | |
method = get_method(test, test_method) | |
try: | |
print(" Test: " + test_method) | |
print(" testing... \n") | |
method() | |
print(" \U00002714\n") | |
print("\U0001F60E \U0001F60E \U0001F60E Success!!\U0001F60E \U0001F60E \U0001F60E\n") | |
except Exception as e: | |
print(" \U0000274C\n") | |
print("\U0001F62D \U0001F62D \U0001F62D Error\U0001F62D \U0001F62D \U0001F62D\n") | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment