How to test a function’s output (stdout/stderr) in unit tests

One thing to also remember, there’s nothing stopping you from writing functions to avoid the boilerplate. For example I have a command line app that uses log and I wrote this function: func captureOutput(f func()) string { var buf bytes.Buffer log.SetOutput(&buf) f() log.SetOutput(os.Stderr) return buf.String() } Then used it like this: output := captureOutput(func() { … Read more

Gradle Test Dependency

You can expose the test classes via a ‘tests’ configuration and then define a testCompile dependency on that configuration. I have this block for all java projects, which jars all test code: task testJar(type: Jar, dependsOn: testClasses) { baseName = “test-${project.archivesBaseName}” from sourceSets.test.output } configurations { tests } artifacts { tests testJar } Then when … Read more

Python unit test with base and sub class

Do not use multiple inheritance, it will bite you later. Instead you can just move your base class into the separate module or wrap it with the blank class: class BaseTestCases: class BaseTest(unittest.TestCase): def testCommon(self): print(‘Calling BaseTest:testCommon’) value = 5 self.assertEqual(value, 5) class SubTest1(BaseTestCases.BaseTest): def testSub1(self): print(‘Calling SubTest1:testSub1’) sub = 3 self.assertEqual(sub, 3) class SubTest2(BaseTestCases.BaseTest): … Read more

Gradle: How to Display Test Results in the Console in Real Time?

Here is my fancy version: import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestLogEvent tasks.withType(Test) { testLogging { // set options for log level LIFECYCLE events TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_OUT exceptionFormat TestExceptionFormat.FULL showExceptions true showCauses true showStackTraces true // set options for log level DEBUG and INFO debug { events TestLogEvent.STARTED, TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_ERROR, TestLogEvent.STANDARD_OUT exceptionFormat TestExceptionFormat.FULL } info.events … Read more

How can I write a test which expects an ‘Error’ to be thrown in Jasmine?

Try using an anonymous function instead: expect( function(){ parser.parse(raw); } ).toThrow(new Error(“Parsing is not possible”)); you should be passing a function into the expect(…) call. Your incorrect code: // incorrect: expect(parser.parse(raw)).toThrow(new Error(“Parsing is not possible”)); is trying to actually call parser.parse(raw) in an attempt to pass the result into expect(…),