1 """
2 Pytest plugin with commonly used fixtures
3 """
4 import os
5 import pytest
6 from ConfigParser import ConfigParser, NoSectionError
7 from tlib.base import LogHelper
8 from tlib.base import TLibHelper
9
10
11
12 @pytest.fixture(scope='class')
13 -def tlib_logger(request):
14 """
15 Returns logger with name tlib.\n
16 This logger should only be used by TLib modules. To log from a test or test module use test_logger\n
17 Logger will get flushed at the end to ensure data is not lost if tests exit abnormally
18 """
19 return LogHelper.get_tlib_logger(request)
20
21
22
23 @pytest.fixture(scope='class')
24 -def adb_logger(request):
25 """
26 Returns logger with name adb\n
27 This logger should only be used by TLib modules interacting with ADB.\n
28 To log from a test or test module use test_logger\n
29 Logger will get flushed at the end to ensure data is not lost if tests exit abnormally
30 """
31 return LogHelper.get_adb_logger(request)
32
33
34
35 @pytest.fixture(scope='class')
36 -def test_logger(request):
37 """
38 Returns logger that can be used to log test information\n
39 Logger will get flushed at the end to ensure data is not lost if tests exit abnormally
40 """
41 return LogHelper.get_test_logger(request)
42
45 """
46 Options supported by pytest selenium plug-in
47 """
48 group = parser.getgroup("tlib", "General command line options provided by TLib")
49
50 group.addoption(
51 "--environment",
52 action="store",
53 dest="environment",
54 help="environment used to run tests")
55
56 group.addoption(
57 "--base_url",
58 action="store",
59 dest="base_url",
60 help="base url of the server used for testing. This can be used for web services as well as web sites")
61
62 group.addoption(
63 "--apk_path",
64 action="store",
65 dest="apk_path",
66 default=None,
67 help="mobile app path of the mobile native app used for testing")
68
69
70
71
72 @pytest.fixture(scope="class")
73 -def base_url(request, tlib_logger):
78
84 """
85 Returns configuration for the environment specified in the --environment command line parameter
86 This function will try to find a file config\config.ini on the current folder or on the parents,
87 up to two levels up.
88
89 This fixture returns a dict like this:
90 {
91 'mysql_host': 'db.ci.qa.ypg.com',
92 'mysql_user': 'ci',
93 'mysql_passwd': '21345'
94 }
95
96 @rtype dict
97 """
98 environment = request.config.getoption("environment")
99
100 if environment is None:
101
102 tlib_logger.error("Parameter --environment was not specified, test can't run")
103 raise RuntimeError("Parameter --environment was not specified, test can't run")
104 return None
105 else:
106
107 curr_folder = os.path.dirname(request.fspath.strpath)
108 config_file = None
109 for i in range(1, 4):
110
111 curr_folder = os.path.abspath(os.path.join(curr_folder, os.pardir))
112
113
114 f = os.path.join(curr_folder, 'config', 'config.ini')
115 if os.path.exists(f):
116 config_file = f
117 break
118
119 if config_file is None:
120 raise IOError("Couldn't find folder 'logs'. Create it under your project folder and try again")
121
122 prj_config = ConfigParser()
123 prj_config.read(config_file)
124
125 try:
126 cfg = prj_config.items("env_" + environment)
127 return dict(cfg)
128 except NoSectionError:
129 tlib_logger.error("There is no section 'env_%s' in config file" % environment)
130 raise RuntimeError("There is no section 'env_%s' in config file" % environment)
131