The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Chapter 14 Assertions and Unit Testing - How to setup?

4 replies on 1 page. Most recent reply: May 27, 2012 2:28 AM by Paul Stivers

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Xiao Yang

Posts: 3
Nickname: xiao
Registered: Aug, 2011

Chapter 14 Assertions and Unit Testing - How to setup? Posted: Aug 3, 2011 9:09 AM
Reply to this message Reply
Advertisement
Hi,

I tried to recreate the last example on p297:
scala> (new ElementSuite).execute()

Instead of getting the output as in the book, I got a different one.

What I did follows:
1. Copy LayoutElement.scala from chp10 and compile it in a separate folder
2. Then specify the path to the compiled package in the interpreter and import all the classes in that package
3. Add scalatest to the path:
:cp ./scalatest-1.6.1.jar
4. Define ElementSuite in the interpreter
5. Run it and I got the following
<-[32m$read$$iw$$iw$$iw$$iw$ElementSuite:<-[0m
<-[32m- testUniformElement<-[0m

It was tested on
- W7_64
- java 1.6.0.25-b06
- scala 2.9.0.1

Could someone let me know what I did wrong? Thank you.


Bill Venners

Posts: 2251
Nickname: bv
Registered: Jan, 2002

Re: Chapter 14 Assertions and Unit Testing - How to setup? Posted: Aug 4, 2011 1:07 AM
Reply to this message Reply
You're seeing three things here. First, in 1.5 and beyond the output of TDD traits were made to look a bit more like the output of BDD traits, by popular demand. The output would normally look like:


ElementSuite:
- testUniformElement


The first thing printed out is the name of the suite of tests. Under that, preceded by a dash, is the name of each test. If it passes, it just has the name. If it fails, it will say test failed and give you a file name and line number (and a stack trace if you request it).

The long, dollar-sign encrusted string before ElementSuite in your case comes because you pasted ElementSuite code into the interpreter, and the interpreter actually assigns it that very long name. The name of your suite case is therefore not ElementSuite but $read$$iw$$iw$$iw$$iw$ElementSuite. Instead of pasting the code into the interpreter, you can put it in a file, then :load that file into the interpreter. Then ElementSuite will have its simple name without the dollar signs.

The other artifacts you are seeing is that by default the output runs with ansi colors turned on, and you're using a terminal window that doesn't support them. To turn them off in the interpreter you can say:


scala> import org.scalatest._

scala> nocolor.run(new ElementSuite)

Xiao Yang

Posts: 3
Nickname: xiao
Registered: Aug, 2011

Re: Chapter 14 Assertions and Unit Testing - How to setup? Posted: Aug 6, 2011 7:50 AM
Reply to this message Reply
Hi Bill,

Thanks for your reply. I put the definition for ElementSuite into a scala file and loaded it into interpreter but the issue of long file name still occurred ($read$...). I am not sure why.

Xiao

Paul Stivers

Posts: 2
Nickname: 81700
Registered: Mar, 2012

Re: Chapter 14 Assertions and Unit Testing - How to setup? Posted: May 27, 2012 2:16 AM
Reply to this message Reply
IMO this would be a good topic to strengthen up in the next version of the book.

1. You can compile the ElementSuite class to get the nicer object name in the scala interpreter.

I found I had to "import org.staircase.layout.Element" to the class. I think the path is consistent with the book, but not sure. I shortened mine to layout.Element.

Also, my file containing the ElementSuite class is named SpiralFunSuiteTest.scala
$ vi SpiralFunSuiteTest.scala

--- snip ---
import org.scalatest.FunSuite
import layout.Element // me added
import Element.elem
class ElementSuite extends FunSuite {
test("elem result should have passed width") {
val ele = elem('x', 3, 3)
expect(2) {
ele.width
}
}
}
--- snip ---

$ scalac SpiralFunSuiteTest.scala

2. Place Layout.scala and Spiral.scala in the same folder with your ElementSuite class and also compile Layout.scala and Spiral.scala

3. In this same folder, start the scala interpreter.

scala> (new ElementSuite).execute()
ElementSuite:
- elem result should have passed width *** FAILED ***

3 did not equal 2 (SpiralFunSuiteTest.scala:7)

4. alternatively you can do the following. I got this from the O'Reilly book.

$ scalac -classpath ../apps/scalatest-1.7.2.jar -d . Layout.scala Spiral.scala SpiralFunSuiteTest.scala
$ scala -classpath ../scalatest-0.9.5.jar org.scalatest.tools.Runner -p . -o -s ElementSuite
Run starting. Expected test count is: 1
ElementSuite:
- elem result should have passed width
Run completed in 125 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 0, ignored 0, pending 0
All tests passed.

--
Paul

Paul Stivers

Posts: 2
Nickname: 81700
Registered: Mar, 2012

Re: Chapter 14 Assertions and Unit Testing - How to setup? Posted: May 27, 2012 2:28 AM
Reply to this message Reply
In my first reply, I ran step 4 with the following, which would pass the test.

-- snip --
test("elem result should have passed width") {
val ele = elem('x', 2, 3)
expect(2) {
ele.width
}
}
-- snip ---

This may have been confusing because my step 3 (I think it was) was run with "elem('x', 3, 3)" which fails the test.

Here's the output if run step 4 with with "elem('x', 3, 3)" to fail the test.

Remember, you have to recompile the ElementSuite class after making the change.

Then:

$ scala -classpath ../scalatest-0.9.5.jar org.scalatest.tools.Runner -p . -o -s ElementSuite
Run starting. Expected test count is: 1
ElementSuite:
- elem result should have passed width *** FAILED ***
Expected 2, but got 3 (SpiralFunSuiteTest.scala:7)
Run completed in 95 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 0, failed 1, ignored 0, pending 0
*** 1 TEST FAILED ***
--
Paul

Flat View: This topic has 4 replies on 1 page
Topic: Scala-based Software Tools Critical for CX Previous Topic   Next Topic Topic: Composable Futures With Akka 2.0


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us