Best Python code snippet using yandex-tank
test_exec_command.py
Source:test_exec_command.py  
...55    # Test posix version:56    with redirect_stdout(StringIO()):57        with redirect_stderr(TemporaryFile()):58            with assert_warns(DeprecationWarning):59                exec_command.exec_command("cd '.'")60    if os.name == 'posix':61        # Test general (non-posix) version:62        with emulate_nonposix():63            with redirect_stdout(StringIO()):64                with redirect_stderr(TemporaryFile()):65                    with assert_warns(DeprecationWarning):66                        exec_command.exec_command("cd '.'")67def test_exec_command_stderr():68    # Test posix version:69    with redirect_stdout(TemporaryFile(mode='w+')):70        with redirect_stderr(StringIO()):71            with assert_warns(DeprecationWarning):72                exec_command.exec_command("cd '.'")73    if os.name == 'posix':74        # Test general (non-posix) version:75        with emulate_nonposix():76            with redirect_stdout(TemporaryFile()):77                with redirect_stderr(StringIO()):78                    with assert_warns(DeprecationWarning):79                        exec_command.exec_command("cd '.'")80class TestExecCommand(object):81    def setup(self):82        self.pyexe = get_pythonexe()83    def check_nt(self, **kws):84        s, o = exec_command.exec_command('cmd /C echo path=%path%')85        assert_(s == 0)86        assert_(o != '')87        s, o = exec_command.exec_command(88         '"%s" -c "import sys;sys.stderr.write(sys.platform)"' % self.pyexe)89        assert_(s == 0)90        assert_(o == 'win32')91    def check_posix(self, **kws):92        s, o = exec_command.exec_command("echo Hello", **kws)93        assert_(s == 0)94        assert_(o == 'Hello')95        s, o = exec_command.exec_command('echo $AAA', **kws)96        assert_(s == 0)97        assert_(o == '')98        s, o = exec_command.exec_command('echo "$AAA"', AAA='Tere', **kws)99        assert_(s == 0)100        assert_(o == 'Tere')101        s, o = exec_command.exec_command('echo "$AAA"', **kws)102        assert_(s == 0)103        assert_(o == '')104        if 'BBB' not in os.environ:105            os.environ['BBB'] = 'Hi'106            s, o = exec_command.exec_command('echo "$BBB"', **kws)107            assert_(s == 0)108            assert_(o == 'Hi')109            s, o = exec_command.exec_command('echo "$BBB"', BBB='Hey', **kws)110            assert_(s == 0)111            assert_(o == 'Hey')112            s, o = exec_command.exec_command('echo "$BBB"', **kws)113            assert_(s == 0)114            assert_(o == 'Hi')115            del os.environ['BBB']116            s, o = exec_command.exec_command('echo "$BBB"', **kws)117            assert_(s == 0)118            assert_(o == '')119        s, o = exec_command.exec_command('this_is_not_a_command', **kws)120        assert_(s != 0)121        assert_(o != '')122        s, o = exec_command.exec_command('echo path=$PATH', **kws)123        assert_(s == 0)124        assert_(o != '')125        s, o = exec_command.exec_command(126             '"%s" -c "import sys,os;sys.stderr.write(os.name)"' %127             self.pyexe, **kws)128        assert_(s == 0)129        assert_(o == 'posix')130    def check_basic(self, *kws):131        s, o = exec_command.exec_command(132                     '"%s" -c "raise \'Ignore me.\'"' % self.pyexe, **kws)133        assert_(s != 0)134        assert_(o != '')135        s, o = exec_command.exec_command(136             '"%s" -c "import sys;sys.stderr.write(\'0\');'137             'sys.stderr.write(\'1\');sys.stderr.write(\'2\')"' %138             self.pyexe, **kws)139        assert_(s == 0)140        assert_(o == '012')141        s, o = exec_command.exec_command(142                 '"%s" -c "import sys;sys.exit(15)"' % self.pyexe, **kws)143        assert_(s == 15)144        assert_(o == '')145        s, o = exec_command.exec_command(146                     '"%s" -c "print(\'Heipa\'")' % self.pyexe, **kws)147        assert_(s == 0)148        assert_(o == 'Heipa')149    def check_execute_in(self, **kws):150        with tempdir() as tmpdir:151            fn = "file"152            tmpfile = os.path.join(tmpdir, fn)153            f = open(tmpfile, 'w')154            f.write('Hello')155            f.close()156            s, o = exec_command.exec_command(157                 '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' %158                 (self.pyexe, fn), **kws)159            assert_(s != 0)160            assert_(o != '')161            s, o = exec_command.exec_command(162                     '"%s" -c "f = open(\'%s\', \'r\'); print(f.read()); '163                     'f.close()"' % (self.pyexe, fn), execute_in=tmpdir, **kws)164            assert_(s == 0)165            assert_(o == 'Hello')166    def test_basic(self):167        with redirect_stdout(StringIO()):168            with redirect_stderr(StringIO()):169                with assert_warns(DeprecationWarning):170                    if os.name == "posix":171                        self.check_posix(use_tee=0)172                        self.check_posix(use_tee=1)173                    elif os.name == "nt":174                        self.check_nt(use_tee=0)175                        self.check_nt(use_tee=1)...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!!
