我想制作一个类似单元测试的框架,该框架允许我为各个规则编写自定义测试。我希望每个测试都在其自己的文件中,即test_R1.clp
将是规则R1的测试文件。每个测试都应该能够加载自己的事实文件。我尝试了以下多种变体,包括对每个文件使用不同的defmodule。在CLIPS中我什至可以做些什么吗?如果是这样,还需要什么才能使它起作用?
我想通过以下方式运行测试:
$CLIPSDOS64.exe -f2 .\test_all.clp
在当前示例中,我得到的错误是[EXPRNPSR3] Missing function declaration for setup-tests.
我已经为每个文件使用唯一的defmodule使单个测试正常工作(即,测试框架为UNITTEST,test_R1文件为R1)。但是,由于在加载文件时或在其他文件中定义了函数时,焦点语句之间的自动切换,我仍然会遇到错误。我看过基本的和高级的CLIPS编程指南,但是如果我在那里错过了什么,请告诉我。
其他特定问题:
(clear)
?TestingFramework.clp:
;;; File: TestingFramework.clp
(defglobal ?*tests-counter* = 0)
(defglobal ?*all-tests-passed* = TRUE)
(defglobal ?*failed-tests-counter* = 0)
(deftemplate test_to_run
(slot testid)
(slot testname)
(slot testsetupfunc)
(slot testcheckfunc))
(deffunction test-check (?test-name ?test-condition)
(if (eval ?test-condition)
then (printout t "SUCCESS: Test " ?test-name crlf)
(printout test_results_file "SUCCESS: Test " ?test-name crlf)
(return TRUE)
else (printout t "FAILURE: Test " ?test-name crlf)
(printout test_results_file "FAILURE: Test " ?test-name crlf)
(return FALSE)))
(deffunction setup_tests ()
(open "test_summary_results.txt" test_results_file "w"))
(deffunction finish_tests ()
(close test_results_file))
(deffunction add_test (?test-name ?test-setup-func ?test-check-func)
(bind ?*tests-counter* (+ 1 ?*tests-counter*))
(assert (test_to_run (testid ?*tests-counter*)
(testname ?test-name)
(testsetupfunc ?test-setup-func)
(testcheckfunc ?test-check-func))))
(deffunction run_all_tests ()
(printout t "About to run " ?*tests-counter* " test(s):" crlf)
(do-for-all-facts ((?ttr_fact test_to_run)) TRUE
(funcall (fact-slot-value ?ttr_fact testsetupfunc))
(if (funcall (fact-slot-value ?ttr_fact testcheckfunc))
then (printout t " SUCCESS" crlf)
else (printout t " FAILURE" crlf)
(bind ?*failed-tests-counter* (+ 1 ?*failed-tests-counter*))
(bind ?*all-tests-passed* FALSE)))
(if ?*all-tests-passed*
then (printout t "All " ?*tests-counter* " tests passed successfully." crlf)
else (printout t ?*failed-tests-counter* "/" ?*tests-counter* " tests failed." crlf)))
tests \ test_R1.clp:
;;; File: test_R1.clp
;;; Tests for Rule 1
(deffunction R1_TEST_1_SETUP ()
(load* "FluidSystem_facts_demo.clp")
(load* "FluidSystem_rules_demo.clp")
(reset))
(deffunction R1_TEST_1 ()
(send [JacketWaterInletTempReading] put-hasValue 35.0)
(send [JacketWaterInletTempReading] put-hasValueDefined DEFINED)
(send [JacketWaterOutletTempReading] put-hasValue 37.0)
(send [JacketWaterOutletTempReading] put-hasValueDefined DEFINED)
(run)
(return (member$ [DissimilarHighTempFlowRate] (send [CounterFlowHeatExchanger] get-hasIssue))))
test_all.clp:
;;; File: test_all.clp
;;; Run tests via:
;;; CLIPSDOS64.exe -f2 .\test_all.clp
(load* "TestingFramework.clp")
(setup-tests)
;;; Test R1
(load* "tests\\test_R1.clp")
(add_test (test_to_run "R1_TEST_1" R1_TEST_1_SETUP R1_TEST_1))
(clear) ;; unsure if this is needed
;;; ... more tests to follow
(run_all_tests)
(finish_tests)
CLIPS回归测试使用的框架可以满足您的需求。您可以从6.4下载目录之一(当前beta版本的sourceforge.net/projects/clipsrules/files/CLIPS/6.40_Beta_3/)中下载它(clips_feature_tests_640.zip)。要运行测试,请在同一目录中启动CLIPS并执行(批处理“ testall.tst”)命令。使用CLIPS终端应用程序,您还可以使用“ clips -f testall.tst”从外壳运行它们。执行完成后,您可以查看结果目录中的* .rsl文件。如果发生差异,则可以使用diff程序将Actual目录中.out文件的内容与Expected目录中的内容进行比较。
该框架使用batch命令自动加载并运行测试用例。 dribble-on命令用于捕获每个测试用例的输出,并将其放置在“实际”目录中的其自己的文件中。该框架允许您运行所有测试用例(使用testall.tst批处理文件),也可以通过运行与测试关联的.tst批处理文件来运行任何单个测试用例。