From f1158457d58d9df8d522f1a128ce467412c52acb Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 18 Mar 2022 21:48:42 +0800 Subject: [PATCH] fix: unittest --- examples/debugtalk.py | 54 +++++++++++++++++++++++++++++++++++++-- internal/scaffold/demo.go | 4 +-- plugin_test.go | 15 ++++++++--- runner_test.go | 12 ++++++--- 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/examples/debugtalk.py b/examples/debugtalk.py index 0ccf6fb7..3d2bb5ff 100644 --- a/examples/debugtalk.py +++ b/examples/debugtalk.py @@ -1,5 +1,55 @@ -from plugin.python.plugin import serve +import logging +from typing import List + +import funppy + + +def sum(*args): + result = 0 + for arg in args: + result += arg + return result + +def sum_ints(*args: List[int]) -> int: + result = 0 + for arg in args: + result += arg + return result + +def sum_two_int(a: int, b: int) -> int: + return a + b + +def sum_two_string(a: str, b: str) -> str: + return a + b + +def sum_strings(*args: List[str]) -> str: + result = "" + for arg in args: + result += arg + return result + +def concatenate(*args: List[str]) -> str: + result = "" + for arg in args: + result += str(arg) + return result + +def setup_hook_example(name): + logging.warning("setup_hook_example") + return f"setup_hook_example: {name}" + +def teardown_hook_example(name): + logging.warning("teardown_hook_example") + return f"teardown_hook_example: {name}" if __name__ == '__main__': - serve() + funppy.register("sum", sum) + funppy.register("sum_ints", sum_ints) + funppy.register("concatenate", concatenate) + funppy.register("sum_two_int", sum_two_int) + funppy.register("sum_two_string", sum_two_string) + funppy.register("sum_strings", sum_strings) + funppy.register("setup_hook_example", setup_hook_example) + funppy.register("teardown_hook_example", teardown_hook_example) + funppy.serve() diff --git a/internal/scaffold/demo.go b/internal/scaffold/demo.go index 90dde3f4..b5410130 100644 --- a/internal/scaffold/demo.go +++ b/internal/scaffold/demo.go @@ -220,11 +220,11 @@ def concatenate(*args: List[str]) -> str: return result def setup_hook_example(name): - logging.warn("setup_hook_example") + logging.warning("setup_hook_example") return f"setup_hook_example: {name}" def teardown_hook_example(name): - logging.warn("teardown_hook_example") + logging.warning("teardown_hook_example") return f"teardown_hook_example: {name}" diff --git a/plugin_test.go b/plugin_test.go index ce68152d..813767f7 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -44,9 +44,18 @@ func TestLocateFile(t *testing.T) { } } -func TestLocatePlugin(t *testing.T) { - // specify target plugin path - _, err := locatePlugin("examples/plugin/debugtalk.py") +func TestLocatePythonPlugin(t *testing.T) { + _, err := locatePlugin("examples/debugtalk.py") + if !assert.Nil(t, err) { + t.Fail() + } +} + +func TestLocateGoPlugin(t *testing.T) { + buildHashicorpPlugin() + defer removeHashicorpPlugin() + + _, err := locatePlugin("examples/debugtalk.bin") if !assert.Nil(t, err) { t.Fail() } diff --git a/runner_test.go b/runner_test.go index 4060df20..b69ef090 100644 --- a/runner_test.go +++ b/runner_test.go @@ -24,10 +24,18 @@ func removeHashicorpPlugin() { os.Remove("examples/debugtalk.bin") } -func TestHttpRunner(t *testing.T) { +func TestHttpRunnerWithGoPlugin(t *testing.T) { buildHashicorpPlugin() defer removeHashicorpPlugin() + assertRunTestCases(t) +} + +func TestHttpRunnerWithPythonPlugin(t *testing.T) { + assertRunTestCases(t) +} + +func assertRunTestCases(t *testing.T) { testcase1 := &TestCase{ Config: NewConfig("TestCase1"). SetBaseURL("http://httpbin.org"), @@ -66,8 +74,6 @@ func TestHttpRunner(t *testing.T) { testcase4 := &demoRefTestCaseJSONPath r := NewRunner(t) - r.saveTests = true - r.genHTMLReport = true err := r.Run(testcase1, testcase2, testcase3, testcase4) if err != nil { t.Fatalf("run testcase error: %v", err)