Junit – TDD Java

All

Intro

I usually do my small prototypes with python and therefore I used pytest and other libraries for as unit test framework. However, I started giving a try on Junit in java. It’s quite simple actually. A good starting tutorial is [1]. I’m a very supporter of TDD and always trying new frameworks and we can see

JUNIT

1. Write the java class –> compile the class

2. Write the java test class –> compile

3. Run test

$JAVA_HOME/bin/java -cp .:junit-4.10.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CalculatorTest

Sucess

JUnit version 4.10
.
Time: 0.002

OK (1 test)

       Failure

JUnit version 4.10
.E
Time: 0.003
There was 1 failure:
1) evaluatesExpression(CalculatorTest)
java.lang.AssertionError: expected:<6> but was:<-6>

FAILURES!!!
Tests run: 1, Failures: 1

 

Obs: I wonder why my tests are taking in avg 1/3 of the time in the original tutorial. 3 times more is quite a different, but it might be the JVM optimization.

PyTest Comparison

Python is a scripting language and therefore does not need to be compiled. So a 1-1 comparison would be decontextualized. However, if we only compare the pytest I think python would win basically the stack is shorter, therefore, simpler to debug actually. Just less noise, but JUNIT is pretty simple also.

The time evaluation is very useful actually.

REFs

[1] https://github.com/junit-team/junit4/wiki/Getting-started

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s