Wednesday, March 23, 2022

Software Automated Testing and Framework 2

Software Automated Testing and Framework 2

Python and bourne shell script

Follow up with the previous tutorial (a href="http://fuyichin.blogspot.com/2022/03/software-automated-testing-and-framework.html">here), testing framework for Java (JUnit) and C (cmocka). This tutorial continue with Python and bourne shell script.

Python

$ edit calculator.py

#!/usr/local/bin/python3
def plus(a,b):
	return a+b
  
$ edit calculator_test.py
import unittest
import calculator

class CalculatorTest(unittest.TestCase):
	def test_plus(self):
		self.assertEqual(51,calculator.plus(20,30))

if __name__ == '__main__':
	unittest.main()
  

Run the test case:
$ python3 calculator_test.py

  F
======================================================================
FAIL: test_plus (__main__.CalculatorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/hooichee/workspace/test_maven/python/calculator_test.py", line 6, in test_plus
    self.assertEqual(51,calculator.plus(20,30))
AssertionError: 51 != 50
  

Change the result and run the test case again.

Ran 1 test in 0.000s
OK

Bourne shell script

I am using Shunit2, can be downloaded from https://github.com/kward/shunit2. There are many other testing framework are available, I just use this as an example.

$ edit calculator.sh

#!/bin/sh
plus() {
	echo `expr $1 + $2`
}
$ edit calculator_test.sh
#!/bin/sh
source calculator.sh

testPlus() {
  assertEquals 100 `plus 99 1`
  assertEquals 'Test plus' 50 `plus 20 30`
}

# Load and run shUnit2.
#. ../shunit2
. /usr/local/bin/shunit2

Run test:

$ sh calculator_test.sh 
testPlus

Ran 1 test.

OK

No comments: