From 1e12e0acce2f01dd1dd139dde33b6555408751b9 Mon Sep 17 00:00:00 2001 From: ste1hi <49638961+ste1hi@users.noreply.github.com> Date: Sat, 15 Jun 2024 17:07:12 +0800 Subject: [PATCH] Change the output checking method in test_func.py. (#18) * Change the output checking method in test_func.py. Use redirect_stdout method in test_func.py. Change the `venv/` into `venv*/` in .gitignore. * Delete a package that is not used in test_func.py --- .gitignore | 2 +- tests/test_func.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 11f27de..5485bba 100755 --- a/.gitignore +++ b/.gitignore @@ -125,7 +125,7 @@ celerybeat.pid .env .venv env/ -venv/ +venv*/ ENV/ env.bak/ venv.bak/ diff --git a/tests/test_func.py b/tests/test_func.py index cc9c64b..37c0a0a 100644 --- a/tests/test_func.py +++ b/tests/test_func.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- +from contextlib import redirect_stdout +import io +import unittest import os -import sys import shutil import unittest from OiRunner import BetterRunner @@ -20,15 +22,12 @@ def tearDown(self): def test_modify_file(self): for i in range(1, 3): - out = sys.stdout with self.assertRaises(SystemExit): - with open("~temp", "w") as f: - sys.stdout = f + with io.StringIO() as buf, redirect_stdout(buf): self.func._modify_file(f"empty{i}.in", "in") - self.assertFalse(os.path.exists("~tmp")) - with open("~temp", "r") as f: - self.assertEqual(f.read(), f"error:empty{i}.in is empty.\n") - sys.stdout = out + self.assertFalse(os.path.exists("~tmp")) + output = buf.getvalue() + self.assertEqual(output, f"error:empty{i}.in is empty.\n") ret_code = self.func._modify_file("a.in", "in") self.assertEqual(ret_code, 3)