Test Cases & Test suites
Give me a chestnut:
- Write the Mystack class simulation stack, and test case writing test;
- Write the file deletion method and delete the test. No more demos, poke this get code
|
Mystack class:
- Public class MYSTATCK {
- Private String[] elements;
- Private int Nextindex;
- Public Mystatck () {
- elements = new string[100];
- Nextindex = 0;
- }
- Public void push (String Element) throws Exception {
- if (Nextindex >= 100) {
- Throw New Exception (" array out of bounds exception! ");
- }
- elements[nextindex++] = element;
- }
- Public String pop () throws Exception {
- if (Nextindex <= 0) {
- Throw New Exception (" array out of bounds exception! ");
- }
- return Elements[--nextindex];
- }
- Public String Top () throws Exception {
- if (Nextindex <= 0) {
- Throw New Exception (" array out of bounds exception! ");
- }
- return elements[nextindex-1];
- }
- }
Write tests on the push method:
- Public void Testpush () {
- Mystatck mystatck = new mystatck ();
- The exception that the method throws is Try-catch treated in the//test case.
- Try {
- Mystatck.push ("Hello world!");
- } catch (Exception e) {
- Assert.fail ("Push method exception, test failed. ");
- }
- String result = null;
- Try {
- result = Mystatck.pop ();
- } catch (Exception e) {
- E.printstacktrace ();
- }
- //Verify the assertion that the pressed string is "Hello world!".
- Assert.assertequals ("Hello world!", result);
- }
Although the Pop method is called in the Testpush test case, a new test case is still created for the pop method:
You can use other methods in the test, or you won't be able to test it.
- Public void Testpop () {
- Mystatck mystatck = new mystatck ();
- Try {
- Mystatck.push ("Hello world!");
- } catch (Exception e) {
- E.printstacktrace ();
- }
- String result = null;
- Try {
- result = Mystatck.pop ();
- } catch (Exception e) {
- Assert.fail ("Pop method exception, test failed. ");
- }
- //Verify that the assertion popup string is "Hello world!".
- Assert.assertequals ("Hello world!", result);
- }
Two test methods are roughly the same, but with a different focus. Focus on the assertion judgment of the test method.
Each test case only does one thing and only tests an aspect.
With the development of projects, there are more and more classes and more tests. The mechanical action of a single test also slows down the speed. Then you need a simpler way to test all of your test cases with just a few points-this is using a test suite.
Test Suite (TestSuite): Multiple tests can be combined to perform multiple tests simultaneously
To create a test suite contract:
- Create a test class within the test source directory;
- Create the public static Test suite () {} method.
Paste Code
- Public class Testall extends TestCase {
- Public Static Test Suite () {
- TestSuite Suite = new TestSuite ();
- Suite.addtestsuite (calctest. Class);
- Suite.addtestsuite (deletealltest. Class);
- Suite.addtestsuite (mystatcktest. Class);
- return Suite;
- }
- }
Run results
Keep the bar green to keep the code Clean--junit detailed (ii)