2015-07-03 25 views
5

Projem için bazı birim testleri bash betikleri olarak yazdım. Haskell'de testleri yazmak için makul bir yol yoktu.Bash testleri ile kabalık kullanımı

cabal test yazarken bu komut dosyalarının çalışmasını istiyorum. Bunu nasıl yaparım?

cevap

1

İlk önce cabal dosyanıza bir test paketi eklemeniz gerekir. Bunun için bu gibi bir şey olacaktır ki, exitcode-stdio test takımı kullanacağız:

Yukarıdaki örnek test paketi sizin test-foo.hs dosyada Sonra the Cabal documentation for test suites

alındı ​​

Name:   foo 
Version:  1.0 
License:  BSD3 
Cabal-Version: >= 1.9.2 
Build-Type:  Simple 

Test-Suite test-foo 
    type:  exitcode-stdio-1.0 
    main-is: test-foo.hs 
    build-depends: base 

Eğer bash komut dosyasını çalıştırmak istiyorum ve sıfır olmayan bir çıkış kodu için bir istisna ile ölür. Sen System.Process kullanarak bunu yapabilirsiniz:

-- test-foo.hs 
import System.Exit (ExitSuccess) 
import System.Process (system) 

main = do 
    -- This dies with a pattern match failure if the shell command fails 
    ExitSuccess <- system "./myprog" 
    return() 

Sonra cabal test kullanarak yukarıdaki testi çalıştırabilir ve kabuk programı sıfırdan farklı çıkış kodu bulunuyorsa o test arızası rapor edecektir.

+0

Sadece kabin konfigürasyonunda mümkün değil mi? Çalışmak istediğim 20 kadar komutum var ve bunların her biri için boş karma programlar oluşturmayı istemiyorum. Benim bir makefile kullanmamdaki çözümüm daha kolay olurdu. –

+0

Hepsi aynı Haskell programında çalıştırabilirsiniz. Her bir betik –

+0

için ayrı bir Haskell programı oluşturmanıza gerek yoktur, ancak daha sonra "cabal testini" çalıştırdığımda, herhangi bir test başarısız olursa, hangisinin başarısız olduğunu söyleyemez. Sadece bash betiklerinden birinin başarısız olduğunu söyleyecektir. –

3

Bu modül, tüm .sh komut dosyalarını test olarak belirli bir alt dizinde çalıştırmanıza izin verir.

cabal test '--test-option=--jxml=dist/test/$test-suite.xml' 

Ve sonra testlerden junit tarzı XML edinebilirsiniz: İsterseniz, siz testini çalıştırmak böylece Ayrıca, bu test-framework paketi kullanır. Bu şu anda my project for testing cabal things'da kontrol edildi. Test kodu:

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) 

benim .cabal dosyasında bu madde ile bu kullanın:

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 

Not ben aynı dizinde .sh testleri ve test modülü yerleştirilir olsa (test denir) o, Bunu yapmak için içsel bir sebep yok.