소개
테스트를 사용하면 코드를 바꿔도 무언가를 망가뜨렸는지 걱정하지 않아도 됩니다. KScript에는 테스트 프레임워크가 내장되어 있어서 따로 설치할 것이 없습니다. 테스트는 이름이 .test.kscript 또는 .test.ks.xml로 끝나는 KScript 파일이며, 보통 tests/ 폴더에 보관하고, kscript test로 실행합니다.
slugify 함수에 대한 테스트 파일은 다음과 같아요:
<!-- tests/strings.test.kscript -->
<kscript>
<import path="../lib/strings.kscript" />
<describe name="slugify">
<test name="lower-cases and joins words with dashes">
<expect value="@slugify('Hello World')" equals="'hello-world'" />
</test>
<let name="cases">[
{ text: 'One', slug: 'one' },
{ text: 'Two Words', slug: 'two-words' },
]</let>
<for each="case" in="$cases">
<test name="slug of '{$case.text}'">
<expect value="@slugify($case.text)" equals="$case.slug" />
</test>
</for>
</describe>
</kscript>// tests/strings.test.kscript
kscript {
import "../lib/strings.kscript"
describe(name: 'slugify') {
test(name: 'lower-cases and joins words with dashes') {
expect(@slugify('Hello World'), equals: 'hello-world')
}
$cases = [
{ text: 'One', slug: 'one' },
{ text: 'Two Words', slug: 'two-words' },
]
for case in $cases {
test(name: "slug of '{$case.text}'") {
expect(@slugify($case.text), equals: $case.slug)
}
}
}
}각 테스트의 결과가 목록으로 표시되며, 실패한 경우에는 무엇이 잘못되었는지도 함께 표시돼요: kscript test을(를) 실행하세요.
$ kscript test
tests/strings.test.kscript
✓ slugify › lower-cases and joins words with dashes (1 ms)
✓ slugify › slug of 'One' (0 ms)
✗ slugify › slug of 'Two Words' (0 ms)
Expected @slugify($case.text) to equal 'two-words', but it was 'two words'.
at tests/strings.test.kscript:14:7
Tests: 1 failed, 2 passed, 3 total
Files: 1 failed, 1 total
Time: 0.08s모든 테스트가 통과하면 종료 코드는 0이에요. 테스트가 실패하거나, 테스트 파일을 실행하지 못하거나, 일치하는 테스트가 없거나, 폴더에 테스트 파일이 없으면 1이에요. 알 수 없는 옵션이나 존재하지 않는 경로처럼 명령줄 또는 kscript.json이 잘못된 경우에는 2예요. 따라서 kscript test로 CI 작업을 게이트할 수 있어요. 종료 코드는 모든 명령에서 동일해요.