Best Python code snippet using yandex-tank
test_config.py
Source:test_config.py  
...90        assert 'bar' in self.cf.describe_option('l', _print_desc=False)91    def test_case_insensitive(self):92        self.cf.register_option('KanBAN', 1, 'doc')93        assert 'doc' in self.cf.describe_option('kanbaN', _print_desc=False)94        assert self.cf.get_option('kanBaN') == 195        self.cf.set_option('KanBan', 2)96        assert self.cf.get_option('kAnBaN') == 297        # gets of non-existent keys fail98        pytest.raises(KeyError, self.cf.get_option, 'no_such_option')99        self.cf.deprecate_option('KanBan')100        assert self.cf._is_deprecated('kAnBaN')101    def test_get_option(self):102        self.cf.register_option('a', 1, 'doc')103        self.cf.register_option('b.c', 'hullo', 'doc2')104        self.cf.register_option('b.b', None, 'doc2')105        # gets of existing keys succeed106        assert self.cf.get_option('a') == 1107        assert self.cf.get_option('b.c') == 'hullo'108        assert self.cf.get_option('b.b') is None109        # gets of non-existent keys fail110        pytest.raises(KeyError, self.cf.get_option, 'no_such_option')111    def test_set_option(self):112        self.cf.register_option('a', 1, 'doc')113        self.cf.register_option('b.c', 'hullo', 'doc2')114        self.cf.register_option('b.b', None, 'doc2')115        assert self.cf.get_option('a') == 1116        assert self.cf.get_option('b.c') == 'hullo'117        assert self.cf.get_option('b.b') is None118        self.cf.set_option('a', 2)119        self.cf.set_option('b.c', 'wurld')120        self.cf.set_option('b.b', 1.1)121        assert self.cf.get_option('a') == 2122        assert self.cf.get_option('b.c') == 'wurld'123        assert self.cf.get_option('b.b') == 1.1124        pytest.raises(KeyError, self.cf.set_option, 'no.such.key', None)125    def test_set_option_empty_args(self):126        pytest.raises(ValueError, self.cf.set_option)127    def test_set_option_uneven_args(self):128        pytest.raises(ValueError, self.cf.set_option, 'a.b', 2, 'b.c')129    def test_set_option_invalid_single_argument_type(self):130        pytest.raises(ValueError, self.cf.set_option, 2)131    def test_set_option_multiple(self):132        self.cf.register_option('a', 1, 'doc')133        self.cf.register_option('b.c', 'hullo', 'doc2')134        self.cf.register_option('b.b', None, 'doc2')135        assert self.cf.get_option('a') == 1136        assert self.cf.get_option('b.c') == 'hullo'137        assert self.cf.get_option('b.b') is None138        self.cf.set_option('a', '2', 'b.c', None, 'b.b', 10.0)139        assert self.cf.get_option('a') == '2'140        assert self.cf.get_option('b.c') is None141        assert self.cf.get_option('b.b') == 10.0142    def test_validation(self):143        self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)144        self.cf.register_option('b.c', 'hullo', 'doc2',145                                validator=self.cf.is_text)146        pytest.raises(ValueError, self.cf.register_option, 'a.b.c.d2',147                      'NO', 'doc', validator=self.cf.is_int)148        self.cf.set_option('a', 2)  # int is_int149        self.cf.set_option('b.c', 'wurld')  # str is_str150        pytest.raises(151            ValueError, self.cf.set_option, 'a', None)  # None not is_int152        pytest.raises(ValueError, self.cf.set_option, 'a', 'ab')153        pytest.raises(ValueError, self.cf.set_option, 'b.c', 1)154        validator = self.cf.is_one_of_factory([None, self.cf.is_callable])155        self.cf.register_option('b', lambda: None, 'doc',156                                validator=validator)157        self.cf.set_option('b', '%.1f'.format)  # Formatter is callable158        self.cf.set_option('b', None)  # Formatter is none (default)159        pytest.raises(ValueError, self.cf.set_option, 'b', '%.1f')160    def test_reset_option(self):161        self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)162        self.cf.register_option('b.c', 'hullo', 'doc2',163                                validator=self.cf.is_str)164        assert self.cf.get_option('a') == 1165        assert self.cf.get_option('b.c') == 'hullo'166        self.cf.set_option('a', 2)167        self.cf.set_option('b.c', 'wurld')168        assert self.cf.get_option('a') == 2169        assert self.cf.get_option('b.c') == 'wurld'170        self.cf.reset_option('a')171        assert self.cf.get_option('a') == 1172        assert self.cf.get_option('b.c') == 'wurld'173        self.cf.reset_option('b.c')174        assert self.cf.get_option('a') == 1175        assert self.cf.get_option('b.c') == 'hullo'176    def test_reset_option_all(self):177        self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)178        self.cf.register_option('b.c', 'hullo', 'doc2',179                                validator=self.cf.is_str)180        assert self.cf.get_option('a') == 1181        assert self.cf.get_option('b.c') == 'hullo'182        self.cf.set_option('a', 2)183        self.cf.set_option('b.c', 'wurld')184        assert self.cf.get_option('a') == 2185        assert self.cf.get_option('b.c') == 'wurld'186        self.cf.reset_option("all")187        assert self.cf.get_option('a') == 1188        assert self.cf.get_option('b.c') == 'hullo'189    def test_deprecate_option(self):190        # we can deprecate non-existent options191        self.cf.deprecate_option('foo')192        assert self.cf._is_deprecated('foo')193        with warnings.catch_warnings(record=True) as w:194            warnings.simplefilter('always')195            try:196                self.cf.get_option('foo')197            except KeyError:198                pass199            else:200                self.fail("Nonexistent option didn't raise KeyError")201            assert len(w) == 1  # should have raised one warning202            assert 'deprecated' in str(w[-1])  # we get the default message203        self.cf.register_option('a', 1, 'doc', validator=self.cf.is_int)204        self.cf.register_option('b.c', 'hullo', 'doc2')205        self.cf.register_option('foo', 'hullo', 'doc2')206        self.cf.deprecate_option('a', removal_ver='nifty_ver')207        with warnings.catch_warnings(record=True) as w:208            warnings.simplefilter('always')209            self.cf.get_option('a')210            assert len(w) == 1  # should have raised one warning211            assert 'eprecated' in str(w[-1])  # we get the default message212            assert 'nifty_ver' in str(w[-1])  # with the removal_ver quoted213            pytest.raises(214                KeyError, self.cf.deprecate_option, 'a')  # can't depr. twice215        self.cf.deprecate_option('b.c', 'zounds!')216        with warnings.catch_warnings(record=True) as w:217            warnings.simplefilter('always')218            self.cf.get_option('b.c')219            assert len(w) == 1  # should have raised one warning220            assert 'zounds!' in str(w[-1])  # we get the custom message221        # test rerouting keys222        self.cf.register_option('d.a', 'foo', 'doc2')223        self.cf.register_option('d.dep', 'bar', 'doc2')224        assert self.cf.get_option('d.a') == 'foo'225        assert self.cf.get_option('d.dep') == 'bar'226        self.cf.deprecate_option('d.dep', rkey='d.a')  # reroute d.dep to d.a227        with warnings.catch_warnings(record=True) as w:228            warnings.simplefilter('always')229            assert self.cf.get_option('d.dep') == 'foo'230            assert len(w) == 1  # should have raised one warning231            assert 'eprecated' in str(w[-1])  # we get the custom message232        with warnings.catch_warnings(record=True) as w:233            warnings.simplefilter('always')234            self.cf.set_option('d.dep', 'baz')  # should overwrite "d.a"235            assert len(w) == 1  # should have raised one warning236            assert 'eprecated' in str(w[-1])  # we get the custom message237        with warnings.catch_warnings(record=True) as w:238            warnings.simplefilter('always')239            assert self.cf.get_option('d.dep') == 'baz'240            assert len(w) == 1  # should have raised one warning241            assert 'eprecated' in str(w[-1])  # we get the custom message242    def test_config_prefix(self):243        with self.cf.config_prefix("base"):244            self.cf.register_option('a', 1, "doc1")245            self.cf.register_option('b', 2, "doc2")246            assert self.cf.get_option('a') == 1247            assert self.cf.get_option('b') == 2248            self.cf.set_option('a', 3)249            self.cf.set_option('b', 4)250            assert self.cf.get_option('a') == 3251            assert self.cf.get_option('b') == 4252        assert self.cf.get_option('base.a') == 3253        assert self.cf.get_option('base.b') == 4254        assert 'doc1' in self.cf.describe_option('base.a', _print_desc=False)255        assert 'doc2' in self.cf.describe_option('base.b', _print_desc=False)256        self.cf.reset_option('base.a')257        self.cf.reset_option('base.b')258        with self.cf.config_prefix("base"):259            assert self.cf.get_option('a') == 1260            assert self.cf.get_option('b') == 2261    def test_callback(self):262        k = [None]263        v = [None]264        def callback(key):265            k.append(key)266            v.append(self.cf.get_option(key))267        self.cf.register_option('d.a', 'foo', cb=callback)268        self.cf.register_option('d.b', 'foo', cb=callback)269        del k[-1], v[-1]270        self.cf.set_option("d.a", "fooz")271        assert k[-1] == "d.a"272        assert v[-1] == "fooz"273        del k[-1], v[-1]274        self.cf.set_option("d.b", "boo")275        assert k[-1] == "d.b"276        assert v[-1] == "boo"277        del k[-1], v[-1]278        self.cf.reset_option("d.b")279        assert k[-1] == "d.b"280    def test_set_ContextManager(self):281        def eq(val):282            assert self.cf.get_option("a") == val283        self.cf.register_option('a', 0)284        eq(0)285        with self.cf.option_context("a", 15):286            eq(15)287            with self.cf.option_context("a", 25):288                eq(25)289            eq(15)290        eq(0)291        self.cf.set_option("a", 17)292        eq(17)293    def test_attribute_access(self):294        holder = []295        def f():296            options.b = 1297        def f2():298            options.display = 1299        def f3(key):300            holder.append(True)301        self.cf.register_option('a', 0)302        self.cf.register_option('c', 0, cb=f3)303        options = self.cf.options304        assert options.a == 0305        with self.cf.option_context("a", 15):306            assert options.a == 15307        options.a = 500308        assert self.cf.get_option("a") == 500309        self.cf.reset_option("a")310        assert options.a == self.cf.get_option("a", 0)311        pytest.raises(KeyError, f)312        pytest.raises(KeyError, f2)313        # make sure callback kicks when using this form of setting314        options.c = 1315        assert len(holder) == 1316    def test_option_context_scope(self):317        # Ensure that creating a context does not affect the existing318        # environment as it is supposed to be used with the `with` statement.319        # See https://github.com/pandas-dev/pandas/issues/8514320        original_value = 60321        context_value = 10322        option_name = 'a'323        self.cf.register_option(option_name, original_value)324        # Ensure creating contexts didn't affect the current context.325        ctx = self.cf.option_context(option_name, context_value)326        assert self.cf.get_option(option_name) == original_value327        # Ensure the correct value is available inside the context.328        with ctx:329            assert self.cf.get_option(option_name) == context_value330        # Ensure the current context is reset331        assert self.cf.get_option(option_name) == original_value332    def test_dictwrapper_getattr(self):333        options = self.cf.options334        # GH 19789335        pytest.raises(self.cf.OptionError, getattr, options, 'bananas')...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
