question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing support for helper classes

See original GitHub issue

Hi, I have seen https://github.com/holgerbrandl/kscript/issues/207 but that seems focused on testing scripts themselves. Testing the kts file is not something I’m interested in because:

  • scripts can be long and complicated
  • they can involve disk and network dependencies that are hard to involve in tests
  • They can require user input
  • Their outputs may not be simple, such as generating files

Instead, I am interested in traditional junit tests for helper functions and classes that the script uses.

I would be open to seeing how to do this outside of kscript - but I feel like kscript might need internal changes to make this work well because:

  • the generated project with --idea would ideally support showing and running the tests
  • Packaging the script (--package) would ideally skip any dependencies that are for test only (such as junit)

While I’m not sure exactly what this would look like, here is one idea I can propose.

Imagine a setup with three files

  • MyScript.kts
  • MyHelper.kt
  • MyHelperTest.kt

Where MyScript uses MyHelper class, and MyHelperTest has tests for the MyHelper class.

The MyHelperTest.kt can declare test dependencies with an annotation indicating it is for testing only.

@file:TestDependsOn("junit:junit:4.12")

MyScript.kts can declare files that contain tests

@file:TestInclude("./MyHelperTest.kt")

Then the nice behavior that kscript could provide is

  • Intellij project created with --idea would put the test files in a test sources folder and add testImplementation dependencies. Tests would run with junit out of the box
  • running the script or packaging it with kscript would skip the test files and dependencies (to speed up compilation and reduce dependency size)
  • A new command like kscript --test could enable easy testing of all of a script’s tests

I could potentially help with this implementation if there is support and alignment! Thank you for the consideration.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
holgerbrandlcommented, Nov 8, 2019

Sure that would be great. If you could potentially also add some bits to the user_guide illustrating the discussed approach for script testing it would be perfect.

1reaction
elihartcommented, Nov 6, 2019

@egor-n thanks for sharing your approach. That seems reasonable if you don’t mind the overhead of creating a permanent gradle project to house your scripts, which I would like to avoid (although maybe it could be a good fallback option)

Read more comments on GitHub >

github_iconTop Results From Across the Web

In TDD, is it bad practice to write helper class for unit tests?
Should testing helper classes be unit tested? Not directly. Helper classes only should exist as product of refactoring of unit tests for ...
Read more >
Unit test helper methods? - java - Stack Overflow
One way is to omit private and put the tests in the same package. Then the tests can call the internal methods but...
Read more >
Unit testing private helper methods - ploeh blog
Evolving a private helper method, guided by tests. A frequently asked question about unit testing and test-driven development (TDD) is how ...
Read more >
Unit Test Good Practice - Developer Documentation
Secondly you should use and extend helper classes (like 1) to provide the workspaces for you. Keep things as generic as you can...
Read more >
Test Helper at XUnitPatterns.com
We define a helper class to hold any Test Utility Methods we want to reuse in several tests. Sketch Test Helper Class embedded...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found