1 """
2 Pytest plugin with commonly used fixtures
3 """
4 import os
5
6 import pytest
7 from ConfigParser import ConfigParser, NoSectionError
8 from tlib.base import LogHelper
9 from tlib.base import TLibHelper
10
11
12
13 @pytest.fixture(scope='class')
14 -def tlib_logger(request):
15 """
16 Returns logger with name tlib.\n
17 This logger should only be used by TLib modules. To log from a test or test module use test_logger\n
18 Logger will get flushed at the end to ensure data is not lost if tests exit abnormally
19 """
20 return LogHelper.get_tlib_logger(request)
21
22
23
24 @pytest.fixture(scope='class')
25 -def adb_logger(request):
26 """
27 Returns logger with name adb\n
28 This logger should only be used by TLib modules interacting with ADB.\n
29 To log from a test or test module use test_logger\n
30 Logger will get flushed at the end to ensure data is not lost if tests exit abnormally
31 """
32 return LogHelper.get_adb_logger(request)
33
34
35
36 @pytest.fixture(scope='class')
37 -def test_logger(request):
38 """
39 Returns logger that can be used to log test information\n
40 Logger will get flushed at the end to ensure data is not lost if tests exit abnormally
41 """
42 return LogHelper.get_test_logger(request)
43
46 """
47 Options supported by pytest selenium plug-in
48 """
49 group = parser.getgroup("tlib", "General command line options provided by TLib")
50
51 group.addoption(
52 "--environment",
53 action="store",
54 dest="environment",
55 help="environment used to run tests")
56
57 group.addoption(
58 "--base_url",
59 action="store",
60 dest="base_url",
61 help="base url of the server used for testing. This can be used for web services as well as web sites")
62
63 group.addoption(
64 "--apk_path",
65 action="store",
66 dest="apk_path",
67 default=None,
68 help="mobile app path of the mobile native app used for testing")
69
70
71
72
73 @pytest.fixture(scope="class")
74 -def base_url(request, tlib_logger):
79
85 """
86 Returns configuration for the environment specified in the --environment command line parameter
87 This function will try to find a file config\config.ini on the current folder or on the parents,
88 up to two levels up.
89
90 This fixture returns a dict like this:
91 {
92 'mysql_host': 'db.ci.qa.ypg.com',
93 'mysql_user': 'ci',
94 'mysql_passwd': '21345'
95 }
96
97 @rtype dict
98 """
99 environment = request.config.getoption("environment")
100
101 if environment is None:
102
103 tlib_logger.error("Parameter --environment was not specified, test can't run")
104 raise RuntimeError("Parameter --environment was not specified, test can't run")
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