def test_run_args(self): def run(): print("I am in run!") job = gg.Job(run=run) job.start() print("run不带参数测试通过") def run(job: Job): print("I am in run!" + job.get_total_score()) job = gg.Job(run=run) job.start() print("run带job参数测试通过") testcase = gg.create_testcase(100) testcase.append("test1", 100) def run(case: TestCases.SingleTestCase): print("I am in run!" + case.name)
def test_job_create(self): try: gg.Job(prework=123) except gg.exception.FunctionsTypeError as e: print("捕获传入函数类型错误成功!" + str(e)) try: gg.Job(run="1231") except gg.exception.FunctionsTypeError as e: print("捕获传入函数类型错误成功!" + str(e)) try: gg.Job(postwork=True) except gg.exception.FunctionsTypeError as e: print("捕获传入函数类型错误成功!" + str(e)) try: gg.Job(prework=None, run=None, postwork=None) print("Job接收空参数任务成功") except FunctionsTypeError as e: raise e def prework(): print("I am from prework!") def run(): print("I an from run!") def postwork(): print("I am from postwork") try: gg.Job(prework=prework, run=run, postwork=postwork) print("Job接收正常参数任务成功") except FunctionsTypeError as e: raise e
def test_prework_agrs(self): def prework(): print("I am in prework!") job = gg.Job(prework=prework) job.start() print("Prework空参数测试通过") def prework(job: Job): print("I am in prework!") job = gg.Job(prework=prework) job.start() print("Prework带参数测试通过") try: def prework(job: Job, asd): print("I am in prework!") job = gg.Job(prework=prework) job.start() except FunctionArgsError: print("Prework多参数异常测试通过")
class TestRun(unittest.TestCase): def test_Run(self): def prework(job: Job): print("I am in prework!") testcases = gg.create_testcase() testcases.append("TestCase1", 20) testcases.append("TestCase2", 20) testcases.append("TestCase3", 20) testcases.append("TestCase4", 20) job.set_testcases(testcases) def run(job: Job, case: TestCases.SingleTestCase): print("I am in run!") time.sleep(0.05) return "123" def postwork(job): print("I am in postwork!") results = job.get_summary() for i in results: print(i) job = gg.Job(prework=prework, run=run, postwork=postwork) start_time = datetime.now() job.start(4) end_time = datetime.now() time_with_thread = (end_time - start_time).total_seconds() start_time = datetime.now() job.start() end_time = datetime.now() time_without_thread = (end_time - start_time).total_seconds() print(f"time_with_thread: {time_with_thread}") print(f"time_without_thread: {time_without_thread}") if time_without_thread // time_with_thread < 3: raise Exception("多线程模式启动失败") job.print() print("测试任务基本运行通过")
def run(job: Job, case: TestCases.SingleTestCase): result = {"verdict": "WA", "score": 0} if case.score == 25: result["verdict"] = "AC" result["score"] = 25 gg.exec("sleep 1", time_out=2) return result def postwork(job: Job): total_score = job.get_total_score() job.score(total_score) if total_score == 100: job.verdict("AC") comment = "" for ret in job.get_summary(): comment += str(ret) job.comment(comment) job.detail("Well Done in detail!") job.secret("Well Done in secret") new_job = gg.Job(prework=prework, run=run, postwork=postwork) new_job.start(max_workers=cpu_core_num) new_job.print()
def test_job_json(self): job = gg.Job() job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Unknown", "score": "0", "rank": {"rank": "-1.0"}, "HTML": "enable"}""" ) job.verdict("Accepted") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "0", "rank": {"rank": "-1.0"}, "HTML": "enable"}""" ) job.score(100) job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable"}""" ) try: job.score(100.00) except DataTypeError: print("检测score字段为浮点数的错误成功") job.score(100) job.rank({"rank": "-1", "test1": "0.5"}) job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0", "test1": "0.5"}, "HTML": "enable"}""" ) try: job.rank({"test1": "0.5"}) except FieldMissingError: print("捕获缺失rank值错误成功!") try: job.rank({"rank": "-1", "test1": "hello"}) except DataTypeError: print("捕获rank值不为浮点数错误成功!") job.rank({"rank": "-1"}) job.comment("test") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable", "comment": "test"}""" ) job.comment("123") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable", "comment": "123"}""" ) print("正确将comment中数字类型转化为字符串") job.comment("") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable"}""" ) print("正确删除comment字段") job.detail("test") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable", "detail": "test"}""" ) job.detail("123") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable", "detail": "123"}""" ) print("正确将detail中数字类型转化为字符串") job.detail("") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable"}""" ) print("正确删除detail字段") job.secret("test") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable", "secret": "test"}""" ) job.secret("123") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable", "secret": "123"}""" ) print("正确将secret中数字类型转化为字符串") job.secret("") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, "HTML": "enable"}""" ) print("正确删除secret字段") job.comment("test") job.detail("test") job.secret("test") job.print() self.assertEqual( job.print(return_str=True), """{"verdict": "Accepted", "score": "100", "rank": {"rank": "-1.0"}, """ + """"HTML": "enable", "comment": "test", "detail": "test", "secret": "test"}""" ) print("正确添加所有字段字段")
print("run不带参数测试通过") def run(job: Job): print("I am in run!" + job.get_total_score()) job = gg.Job(run=run) job.start() print("run带job参数测试通过") testcase = gg.create_testcase(100) testcase.append("test1", 100) def run(case: TestCases.SingleTestCase): print("I am in run!" + case.name) job = gg.Job(run=run) job.set_testcases(testcase) job.start() print("run带test参数测试通过") def run(job: Job, case: TestCases.SingleTestCase): print("I am in run!" + case.name) job = gg.Job(run=run) job.set_testcases(testcase) job.start() print("run带两个参数测试通过") try: def run(job: Job, case: TestCases.SingleTestCase, qwewqe):