对于我的项目,我已经将一些单元测试写成了bash脚本.确实没有合理的方法在
Haskell中编写测试.
我想在输入cabal测试时运行这些脚本.我该如何做到这一点?
此模块允许您在特定子目录中运行所有.sh脚本作为测试.此外,这使用了测试框架包,因此如果需要,您可以运行测试:
cabal test '--test-option=--jxml=dist/test/$test-suite.xml'
然后,您可以从测试中获取junit样式的XML.目前已在my project for testing cabal things中签入.测试代码:
import Data.List (isSuffixOf) import Control.Applicative import Test.Framework (defaultMain,testGroup,Test) import Test.Framework.Providers.HUnit import Test.HUnit (assertFailure) import System.Directory import System.Exit (ExitCode(..)) import System.Process main :: IO () main = makeTests "test" >>= defaultMain -- Make a test out of those things which end in ".sh" and are executable -- Make a testgroup out of directories makeTests :: FilePath -> IO [Test] makeTests dir = do origDir <- getCurrentDirectory contents <- getDirectoryContents dir setCurrentDirectory dir retval <- mapM fileFunc contents setCurrentDirectory origDir return $concat retval where fileFunc "." = return [] fileFunc ".." = return [] fileFunc f | ".sh" `isSuffixOf` f = do fullName <- canonicalizePath f isExecutable <- executable <$> getPermissions fullName let hunitTest = mkTest fullName return [testCase f hunitTest | isExecutable] fileFunc d = do fullName <- canonicalizePath d isSearchable <- searchable <$> getPermissions fullName if isSearchable then do subTests <- makeTests d return [testGroup d subTests] else return [] mkTest fullName = do execResult <- system fullName case execResult of ExitSuccess -> return () ExitFailure code -> assertFailure ("Failed with code " ++ show code)
我在.cabal文件中使用此子句:
test-suite BackflipShellTests type: exitcode-stdio-1.0 main-is: BackflipShellTests.hs hs-source-dirs: test build-depends: backflip,base,test-framework-hunit,test-framework,directory,process,HUnit default-language: Haskell2010
请注意,虽然我将.sh测试和测试模块放在同一目录(称为测试)中,但没有固有的理由这样做.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。