How to use StdCaptureFD method in Pytest

Best Python code snippet using pytest

test_capture.py

Source:test_capture.py Github

copy

Full Screen

...302 kw['now'] = False303 cap = py.io.StdCapture(**kw)304 cap.startall()305 return cap306class TestStdCaptureFD(TestStdCapture):307 pytestmark = needsdup308 def getcapture(self, **kw):309 return py.io.StdCaptureFD(**kw)310 def test_intermingling(self):311 cap = self.getcapture()312 oswritebytes(1, "1")313 sys.stdout.write(str(2))314 sys.stdout.flush()315 oswritebytes(1, "3")316 oswritebytes(2, "a")317 sys.stderr.write("b")318 sys.stderr.flush()319 oswritebytes(2, "c")320 out, err = cap.reset()321 assert out == "123"322 assert err == "abc"323 def test_callcapture(self):324 def func(x, y):325 print (x)326 py.std.sys.stderr.write(str(y))327 return 42328 res, out, err = py.io.StdCaptureFD.call(func, 3, y=4)329 assert res == 42330 assert out.startswith("3")331 assert err.startswith("4")332 def test_many(self, capfd):333 def f():334 for i in range(10):335 cap = py.io.StdCaptureFD()336 cap.reset()337 lsof_check(f)338class TestStdCaptureFDNotNow(TestStdCaptureFD):339 pytestmark = needsdup340 def getcapture(self, **kw):341 kw['now'] = False342 cap = py.io.StdCaptureFD(**kw)343 cap.startall()344 return cap345@needsdup346def test_stdcapture_fd_tmpfile(tmpfile):347 capfd = py.io.StdCaptureFD(out=tmpfile)348 os.write(1, "hello".encode("ascii"))349 os.write(2, "world".encode("ascii"))350 outf, errf = capfd.done()351 assert outf == tmpfile352class TestStdCaptureFDinvalidFD:353 pytestmark = needsdup354 def test_stdcapture_fd_invalid_fd(self, testdir):355 testdir.makepyfile("""356 import py, os357 def test_stdout():358 os.close(1)359 cap = py.io.StdCaptureFD(out=True, err=False, in_=False)360 cap.done()361 def test_stderr():362 os.close(2)363 cap = py.io.StdCaptureFD(out=False, err=True, in_=False)364 cap.done()365 def test_stdin():366 os.close(0)367 cap = py.io.StdCaptureFD(out=False, err=False, in_=True)368 cap.done()369 """)370 result = testdir.runpytest("--capture=fd")371 assert result.ret == 0372 assert result.parseoutcomes()['passed'] == 3373def test_capture_not_started_but_reset():374 capsys = py.io.StdCapture(now=False)375 capsys.done()376 capsys.done()377 capsys.reset()378@needsdup379def test_capture_no_sys():380 capsys = py.io.StdCapture()381 try:382 cap = py.io.StdCaptureFD(patchsys=False)383 sys.stdout.write("hello")384 sys.stderr.write("world")385 oswritebytes(1, "1")386 oswritebytes(2, "2")387 out, err = cap.reset()388 assert out == "1"389 assert err == "2"390 finally:391 capsys.reset()392@needsdup393def test_callcapture_nofd():394 def func(x, y):395 oswritebytes(1, "hello")396 oswritebytes(2, "hello")397 print (x)398 sys.stderr.write(str(y))399 return 42400 capfd = py.io.StdCaptureFD(patchsys=False)401 try:402 res, out, err = py.io.StdCapture.call(func, 3, y=4)403 finally:404 capfd.reset()405 assert res == 42406 assert out.startswith("3")407 assert err.startswith("4")408@needsdup409@py.test.mark.multi(use=[True, False])410def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):411 if not use:412 tmpfile = True413 cap = py.io.StdCaptureFD(out=False, err=tmpfile, now=False)414 cap.startall()415 capfile = cap.err.tmpfile416 cap.suspend()417 cap.resume()418 capfile2 = cap.err.tmpfile419 assert capfile2 == capfile420@py.test.mark.multi(method=['StdCapture', 'StdCaptureFD'])421def test_capturing_and_logging_fundamentals(testdir, method):422 if method == "StdCaptureFD" and not hasattr(os, 'dup'):423 py.test.skip("need os.dup")424 # here we check a fundamental feature425 p = testdir.makepyfile("""426 import sys, os427 import py, logging...

Full Screen

Full Screen

test_apps.py

Source:test_apps.py Github

copy

Full Screen

...38 get.assert_called_with("target/apps", headers=TestAuthHeader)39 @mock.patch("requests.get")40 def test_ListAppsWithoutLogin(self, get):41 am = AppManager("target", self.dbn)42 capture = py.io.StdCaptureFD(in_=False)43 am.list()44 out, err = capture.reset()45 get.not_called()46 self.assertEquals(out.strip(), ELoginFirst)47 @mock.patch("requests.get")48 def test_GetAppWithLogin(self, get):49 self.loggedin()50 am = AppManager("target", self.dbn)51 am.get("myapp")52 get.assert_called_with("target/apps/myapp", headers=TestAuthHeader)53 @mock.patch("requests.get")54 def test_GetAppWithoutLogin(self, get):55 am = AppManager("target", self.dbn)56 capture = py.io.StdCaptureFD(in_=False)57 am.get("myapp")58 out, err = capture.reset()59 get.not_called()60 self.assertEquals(out.strip(), ELoginFirst)61 @mock.patch("requests.delete")62 def test_RemoveAppWithLogin(self, delete):63 self.loggedin()64 am = AppManager("target", self.dbn)65 capture = py.io.StdCaptureFD(in_=False)66 am.remove("myapp")67 out, err = capture.reset()68 delete.assert_called_with("target/apps/myapp", headers=TestAuthHeader)69 self.assertEquals(out.strip(), IRemoveApp)70 @mock.patch("requests.delete")71 def test_RemoveAppWithoutLogin(self, delete):72 am = AppManager("target", self.dbn)73 capture = py.io.StdCaptureFD(in_=False)74 am.remove("myapp")75 out, err = capture.reset()76 delete.not_called()77 self.assertEquals(out.strip(), ELoginFirst)78 @mock.patch("requests.post")79 def test_CreateAppWithLogin(self, post):80 self.loggedin()81 am = AppManager("target", self.dbn)82 data = {83 "name": "myapp",84 "framework": "python2.7",85 "units": 586 }87 b = json.dumps(data)88 capture = py.io.StdCaptureFD(in_=False)89 am.create("myapp", "python2.7", 5)90 out, err = capture.reset()91 post.assert_called_with("target/apps", data=b, headers=TestAuthHeader)92 self.assertEquals(out.strip(), ICreateApp)93 @mock.patch("requests.post")94 def test_CreateAppWithoutLogin(self, post):95 am = AppManager("target", self.dbn)96 data = {97 "name": "myapp",98 "framework": "python2.7",99 "units": 5100 }101 b = json.dumps(data)102 capture = py.io.StdCaptureFD(in_=False)103 am.create("myapp", "python2.7", 5)104 out, err = capture.reset()105 post.not_called()106 self.assertEquals(out.strip(), ELoginFirst)107 @mock.patch("requests.put")108 def test_AddUnitWithLogin(self, put):109 self.loggedin()110 am = AppManager("target", self.dbn)111 capture = py.io.StdCaptureFD(in_=False)112 am.unitadd("myapp", 5)113 out, err = capture.reset()114 put.assert_called_with("target/apps/myapp/units", data=str(5), headers=TestAuthHeader)115 self.assertEquals(out.strip(), IAddUnit)116 @mock.patch("requests.put")117 def test_AddUnitWithoutLogin(self, put):118 am = AppManager("target", self.dbn)119 capture = py.io.StdCaptureFD(in_=False)120 am.unitadd("myapp", 5)121 out, err = capture.reset()122 put.not_called()123 self.assertEquals(out.strip(), ELoginFirst)124 @mock.patch("requests.delete")125 def test_RemoveUnitWithLogin(self, delete):126 self.loggedin()127 am = AppManager("target", self.dbn)128 capture = py.io.StdCaptureFD(in_=False)129 am.unitremove("myapp", 5)130 out, err = capture.reset()131 delete.assert_called_with("target/apps/myapp/units", data=str(5), headers=TestAuthHeader)132 self.assertEquals(out.strip(), IRemoveUnit)133 @mock.patch("requests.delete")134 def test_RemoveUnitWithoutLogin(self, delete):135 am = AppManager("target", self.dbn)136 capture = py.io.StdCaptureFD(in_=False)137 am.unitremove("myapp", 5)138 out, err = capture.reset()139 #out, err = capsys.readouterr()140 delete.not_called()141 self.assertEquals(out.strip(), ELoginFirst)...

Full Screen

Full Screen

Pytest Tutorial

Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.

Chapters

  1. What is pytest
  2. Pytest installation: Want to start pytest from scratch? See how to install and configure pytest for Python automation testing.
  3. Run first test with pytest framework: Follow this step-by-step tutorial to write and run your first pytest script.
  4. Parallel testing with pytest: A hands-on guide to parallel testing with pytest to improve the scalability of your test automation.
  5. Generate pytest reports: Reports make it easier to understand the results of pytest-based test runs. Learn how to generate pytest reports.
  6. Pytest Parameterized tests: Create and run your pytest scripts while avoiding code duplication and increasing test coverage with parameterization.
  7. Pytest Fixtures: Check out how to implement pytest fixtures for your end-to-end testing needs.
  8. Execute Multiple Test Cases: Explore different scenarios for running multiple test cases in pytest from a single file.
  9. Stop Test Suite after N Test Failures: See how to stop your test suite after n test failures in pytest using the @pytest.mark.incremental decorator and maxfail command-line option.

YouTube

Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.

https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP

Run Pytest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful