160 likes | 487 Views
JUnit. Team segfault Arjun Bhasin Chakori macherla Gunjan raghav Jaideep singh Vicky sehrawat. What is JUnit ?. JUnit is a regression testing framework Used for unit testing framework in Java programs User interface for viewing test results Text GUI
E N D
JUnit Team segfault ArjunBhasin Chakorimacherla Gunjanraghav Jaideepsingh Vicky sehrawat
What is JUnit? • JUnit is a regression testing framework • Used for unit testing framework in Java programs • User interface for viewing test results • Text • GUI • Integrated with several IDEslike Eclipse • It is one of a family of unit testing frameworks collectively known as xUnit that originated with SUnit.
Who developed JUnit? Written by Erich Gamma and Kent Beck. Used by developers to implement unit tests in Java
Why JUnit? • Test framework provides tools for: • Assertions • Running tests • Aggregating Tests (suites) • Reporting Results • Less reliance on time consuming debuggers • JUnit improves our code quality
Features! JUnit features include: Assertions for testing expected results Test fixtures for sharing common test data Test suites for easily organizing and running tests Graphical and textual test runners
Limitations Impossible to test a client/server environment, or even a peer-to-peer scenario. We cannot perform distributed tests, so you don’t have a way to run the same series of tests simultaneously on several JVMs each running on different Operating Systems.
Setup and Teardown • Setup: • Use the @Before annotation on a method containing code to run before each test case. • Teardown: • Use the @After annotation on a method containing code to run after each test case. • These methods will run even if exceptions are thrown in the test case or an assertion fails. • It is allowed to have any number of these annotations. • All methods annotated with @Before will be run before each test case, but they may be run in any order.
Example: Using a file as a text fixture public class OutputTest { private File output; @Before public void createOutputFile() { output = new File(...); } @After public void deleteOutputFile() { output.delete(); } @Test public void test1WithFile() { // code for test case objective } @Test public void test2WithFile() { // code for test case objective } }
Method execution order • createOutputFile() • test1WithFile() • deleteOutputFile() • createOutputFile() • test2WithFile() • deleteOutputFile() • Assumption: test1WithFile runs before test2WithFile.
Adding Tests to TestCases • Any method in a TestCase class is considered a test if it begins with the word test • You can write many tests (have many test methods) • Each test method should use a variety of assert methods to test things about the state of their classes under tests • Assert methods are inherited
Assert Methods • Assert methods include: • assertEqual(x,y) • assertFalse(boolean) • assertTrue(boolean) • assertNull(object) • assertNotNull(object) • assetSame(firstObject, secondObject) • assertNotSame(firstObject, secondObject)
Adding Two Tests to TestCase package testing; importjunit.framework.TestCase; publicclassFirstTestCaseextendsTestCase { publicFirstTestCase(String arg0) { super(arg0); } publicstaticvoid main(String[] args) {} protectedvoidsetUp() throws Exception { super.setUp(); } protectedvoidtearDown() throws Exception { super.tearDown(); } publicvoidtestCompareSucceed() { assertEquals(0, 0); //this assertion will succeed } publicvoidtestCompareFail() { assertEquals(0, 1); //this assertion will fail } }
Sources https://staff.ti.bfh.ch/fileadmin/home/due1/uml_dp/JUnitTutorial-Williams-adapted.pdf http://vishnuagrawal.wordpress.com/2007/04/30/limitation-of-junit/ http://en.wikipedia.org/wiki/JUnit http://www.junit.org/