command line tool testing: capturing stdout and stderr

51 views
Skip to first unread message

Jaakko Suutarla

unread,
Apr 27, 2015, 2:23:32 AM4/27/15
to clojure...@googlegroups.com
Hi,

I would like to write end to end tests for my command line tool and I am trying to figure ways to do it.
What would be the best way to test for code thats checks stdout output? 

Example usage case: 

Code:
...

(defn magical-cli-command [argument1]
  ...
 
 
(println "That was nice command and output is: " foober " and " barfoo))

...

Test case:
(deftest test-cli-output
 
(testing "a test"
    (magical-cli-command 1 2)
    (is (= (captured-stdout-here-some-how) "That was nice command and output is: 1 and 2"))))

 

Some ideas:
  • with-redefs println - feels little dirty
  • using binding [*out* - not sure if good idea
Any ideas what would be the best way to do it?

Cheers,
Jaakko

Pavel Prokopenko

unread,
Apr 27, 2015, 2:44:00 AM4/27/15
to clojure...@googlegroups.com
Hi,

you can use https://clojuredocs.org/clojure.core/with-out-str to verify the output which is basically the second option.

Pavel

Vesa Marttila

unread,
Apr 27, 2015, 4:07:34 AM4/27/15
to clojure...@googlegroups.com
Hi Jaakko,

Would separating the side-effecting functionality (printing) be plausible in this case? If so, it would mean that you could have a unit test for the actual logic.

(defn actual-logic
 
[argument1]
 
...
 
[foober barfoo])

(defn magical-cli-command [argument1]
  (let [[foober barfoo] (actual-logic argument1)]
   
(println "That was nice command and output is: " foober " and " barfoo)))

(deftest test-actual-logic
 
(testing "a test"
    (is (= [... ...] (actual-logic 1 2))))


Vesa

Jaakko Suutarla

unread,
Apr 27, 2015, 5:47:36 AM4/27/15
to clojure...@googlegroups.com
Hi Vesa & Pavel,

Thanks for the quick replies @Vesa and @Pavel

@Vesa
I have already few test sets for internal logic, but I want to ensures that while interacting with it from CLI it will work as I assume. ;)

@Pavel
Thanks I'll try to wrap my test cases with with-out-str

Jaakko
Reply all
Reply to author
Forward
0 new messages