You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/user_guide.md
+45Lines changed: 45 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ Feel welcome to submit a [ticket](https://github.com/holgerbrandl/kscript/issues
17
17
-[Create interpreters for custom DSLs](#create-interpreters-for-custom-dsls)
18
18
-[Tips and tricks](#tips-and-tricks)
19
19
-[Display images inline and open other files](#display-images-inline-and-open-other-files)
20
+
-[Testing](#testing)
20
21
-[Text Processing](#text-processing)
21
22
-[Examples](#examples)
22
23
-[Bioinformatics](#bioinformatics)
@@ -114,7 +115,51 @@ When using iterm2 it is possible to print inlined image output directly into the
114
115
115
116
Suggested by [@yschimke](https://github.com/yschimke) in [#51](https://github.com/holgerbrandl/kscript/issues/51)
116
117
118
+
## Testing
119
+
When an IDEA project is generated with the `--idea` command it is configured to consider all project files as test sources.
117
120
121
+
This means that you can include unit test code, such as with JUnit, in your script files and run the tests with Gradle.
122
+
123
+
For example, to run JUnit tests you would include the junit dependency at the top of your Kotlin file:
124
+
```kotlin
125
+
@file:DependsOn("junit:junit:4.12")
126
+
```
127
+
128
+
and then include a class with your tests somewhere in that file:
129
+
```kotlin
130
+
classMyTests {
131
+
@Test
132
+
funmyTest() {
133
+
check(1+1==2)
134
+
}
135
+
}
136
+
```
137
+
138
+
You can run this test like usual, either from the IDE (by right clicking on the function or class and selecting "run") or from the command line (with `./gradlew test`).
139
+
140
+
### Caveats
141
+
- These tests can go in either .kts files or the .kt files that your script depend on via `@file:Include`. However, tests in a `.kts` file can only be run from the command line
142
+
- If you encounter any Gradle build errors such as "Duplicate JVM class name" then try cleaning the project first - `./gradlew clean test`
143
+
- Kscript does not differentiate test dependencies, so any test code or test imports in your script will be included when creating a packaged binary with `--package`
144
+
145
+
### Testing on CI
146
+
If you have script tests that you would like to run in automated environments, like CI, then you likely don't want to open up an Intellij IDEA instance to do so.
147
+
148
+
However, to setup the gradle project to run the tests we still need to use the `--idea` command. To prevent this command from failing, you can create a mock `idea`:
149
+
```
150
+
touch idea && chmod +x idea
151
+
```
152
+
153
+
The `--idea` command outputs the directory that the project was created in, which you can use to determine where to run your tests.
154
+
155
+
For example, you could do something like this to generate the project and test it in one command.
156
+
```bash
157
+
project_path=$(kscript --idea YourScript.kts 2>&1| grep --line-buffered "Project set up at"| cut -d'/' -f2-)
158
+
cd"/$project_path"
159
+
gradle test
160
+
```
161
+
162
+
Note that the Gradlew wrapper is not automatically created by `kscript --idea` so `./gradlew` will not be available. This uses a gradle instance from the PATH instead.
0 commit comments