How to use lsof_check method in Pytest

Best Python code snippet using pytest

test_capture.py

Source:test_capture.py Github

copy

Full Screen

...93 tmpfile = SomeFileWrapper()94 assert py.io.dupfile(tmpfile) is tmpfile95 with py.test.raises(AttributeError):96 py.io.dupfile(tmpfile, raising=True)97def lsof_check(func):98 pid = os.getpid()99 try:100 out = py.process.cmdexec("lsof -p %d" % pid)101 except py.process.cmdexec.Error:102 py.test.skip("could not run 'lsof'")103 func()104 out2 = py.process.cmdexec("lsof -p %d" % pid)105 len1 = len([x for x in out.split("\n") if "REG" in x])106 len2 = len([x for x in out2.split("\n") if "REG" in x])107 assert len2 < len1 + 3, out2108class TestFDCapture:109 pytestmark = needsdup110 def test_not_now(self, tmpfile):111 fd = tmpfile.fileno()112 cap = py.io.FDCapture(fd, now=False)113 data = tobytes("hello")114 os.write(fd, data)115 f = cap.done()116 s = f.read()117 assert not s118 cap = py.io.FDCapture(fd, now=False)119 cap.start()120 os.write(fd, data)121 f = cap.done()122 s = f.read()123 assert s == "hello"124 def test_simple(self, tmpfile):125 fd = tmpfile.fileno()126 cap = py.io.FDCapture(fd)127 data = tobytes("hello")128 os.write(fd, data)129 f = cap.done()130 s = f.read()131 assert s == "hello"132 f.close()133 def test_simple_many(self, tmpfile):134 for i in range(10):135 self.test_simple(tmpfile)136 def test_simple_many_check_open_files(self, tmpfile):137 lsof_check(lambda: self.test_simple_many(tmpfile))138 def test_simple_fail_second_start(self, tmpfile):139 fd = tmpfile.fileno()140 cap = py.io.FDCapture(fd)141 f = cap.done()142 py.test.raises(ValueError, cap.start)143 f.close()144 def test_stderr(self):145 cap = py.io.FDCapture(2, patchsys=True)146 print_("hello", file=sys.stderr)147 f = cap.done()148 s = f.read()149 assert s == "hello\n"150 def test_stdin(self, tmpfile):151 tmpfile.write(tobytes("3"))152 tmpfile.seek(0)153 cap = py.io.FDCapture(0, tmpfile=tmpfile)154 # check with os.read() directly instead of raw_input(), because155 # sys.stdin itself may be redirected (as py.test now does by default)156 x = os.read(0, 100).strip()157 f = cap.done()158 assert x == tobytes("3")159 def test_writeorg(self, tmpfile):160 data1, data2 = tobytes("foo"), tobytes("bar")161 try:162 cap = py.io.FDCapture(tmpfile.fileno())163 tmpfile.write(data1)164 cap.writeorg(data2)165 finally:166 tmpfile.close()167 f = cap.done()168 scap = f.read()169 assert scap == totext(data1)170 stmp = open(tmpfile.name, 'rb').read()171 assert stmp == data2172class TestStdCapture:173 def getcapture(self, **kw):174 return py.io.StdCapture(**kw)175 def test_capturing_done_simple(self):176 cap = self.getcapture()177 sys.stdout.write("hello")178 sys.stderr.write("world")179 outfile, errfile = cap.done()180 s = outfile.read()181 assert s == "hello"182 s = errfile.read()183 assert s == "world"184 def test_capturing_reset_simple(self):185 cap = self.getcapture()186 print("hello world")187 sys.stderr.write("hello error\n")188 out, err = cap.reset()189 assert out == "hello world\n"190 assert err == "hello error\n"191 def test_capturing_readouterr(self):192 cap = self.getcapture()193 try:194 print ("hello world")195 sys.stderr.write("hello error\n")196 out, err = cap.readouterr()197 assert out == "hello world\n"198 assert err == "hello error\n"199 sys.stderr.write("error2")200 finally:201 out, err = cap.reset()202 assert err == "error2"203 def test_capturing_readouterr_unicode(self):204 cap = self.getcapture()205 print ("hx\xc4\x85\xc4\x87")206 out, err = cap.readouterr()207 assert out == py.builtin._totext("hx\xc4\x85\xc4\x87\n", "utf8")208 @py.test.mark.skipif('sys.version_info >= (3,)',209 reason='text output different for bytes on python3')210 def test_capturing_readouterr_decode_error_handling(self):211 cap = self.getcapture()212 # triggered a internal error in pytest213 print('\xa6')214 out, err = cap.readouterr()215 assert out == py.builtin._totext('\ufffd\n', 'unicode-escape')216 def test_capturing_mixed(self):217 cap = self.getcapture(mixed=True)218 sys.stdout.write("hello ")219 sys.stderr.write("world")220 sys.stdout.write(".")221 out, err = cap.reset()222 assert out.strip() == "hello world."223 assert not err224 def test_reset_twice_error(self):225 cap = self.getcapture()226 print ("hello")227 out, err = cap.reset()228 py.test.raises(ValueError, cap.reset)229 assert out == "hello\n"230 assert not err231 def test_capturing_modify_sysouterr_in_between(self):232 oldout = sys.stdout233 olderr = sys.stderr234 cap = self.getcapture()235 sys.stdout.write("hello")236 sys.stderr.write("world")237 sys.stdout = py.io.TextIO()238 sys.stderr = py.io.TextIO()239 print ("not seen")240 sys.stderr.write("not seen\n")241 out, err = cap.reset()242 assert out == "hello"243 assert err == "world"244 assert sys.stdout == oldout245 assert sys.stderr == olderr246 def test_capturing_error_recursive(self):247 cap1 = self.getcapture()248 print ("cap1")249 cap2 = self.getcapture()250 print ("cap2")251 out2, err2 = cap2.reset()252 out1, err1 = cap1.reset()253 assert out1 == "cap1\n"254 assert out2 == "cap2\n"255 def test_just_out_capture(self):256 cap = self.getcapture(out=True, err=False)257 sys.stdout.write("hello")258 sys.stderr.write("world")259 out, err = cap.reset()260 assert out == "hello"261 assert not err262 def test_just_err_capture(self):263 cap = self.getcapture(out=False, err=True)264 sys.stdout.write("hello")265 sys.stderr.write("world")266 out, err = cap.reset()267 assert err == "world"268 assert not out269 def test_stdin_restored(self):270 old = sys.stdin271 cap = self.getcapture(in_=True)272 newstdin = sys.stdin273 out, err = cap.reset()274 assert newstdin != sys.stdin275 assert sys.stdin is old276 def test_stdin_nulled_by_default(self):277 print ("XXX this test may well hang instead of crashing")278 print ("XXX which indicates an error in the underlying capturing")279 print ("XXX mechanisms")280 cap = self.getcapture()281 py.test.raises(IOError, "sys.stdin.read()")282 out, err = cap.reset()283 def test_suspend_resume(self):284 cap = self.getcapture(out=True, err=False, in_=False)285 try:286 print ("hello")287 sys.stderr.write("error\n")288 out, err = cap.suspend()289 assert out == "hello\n"290 assert not err291 print ("in between")292 sys.stderr.write("in between\n")293 cap.resume()294 print ("after")295 sys.stderr.write("error_after\n")296 finally:297 out, err = cap.reset()298 assert out == "after\n"299 assert not err300class TestStdCaptureNotNow(TestStdCapture):301 def getcapture(self, **kw):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 == tmpfile...

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