Table of Contents |
---|
Excerpt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TestingTo ensure reliability and stability of Safety Analysis a variety of tests are performed. We are using JUnit, Pi-test and other Java extensons connected with tests. Actually we started to introduce Cucumber as a new tool for acceptance tests. During execution of all tests bugs and issues are found. Analysis of tests results is available here. Acceptance testsSoftware testing method conducted to determine if the requirements of a specification or contract are met. It may involve User Acceptance Testing (UAT), Operational Acceptance Testing (OAT), Acceptance testing in extreme programming, alpha and beta testing. You write acceptance tests to check if your code is passing the requirements of project. You should run these tests in integration-test phase.Naming param is *prefix*AT.java. Component testsComponent testing is a method where testing of each component in an application is done separately. Suppose, in an application there are 5 components. Testing of each 5 components separately and efficiently is called as component testing. It finds the defects in the module and verifies the functioning of software. Component tests use spring context or in memory database. Naming param is *className*IT.java. Unit testsSoftware testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.Unit testing is commonly automated, but may still be performed manually. The objective in unit testing is to isolate a unit and validate its correctness. You should write JUnit to check every function that you use in your code. JUnit must be fast, it should be matters of seconds to run that test. JUnit should test only define unit of system. Naming param is *className*Test.java. Command Line Use Case Tests SpecificationThis page contains documentation for CLI Use Case Tests in order of appearance in code.
CucumberCucumber is a tool that we use for write acceptable tests. It runs automated acceptance tests written in a behaviour-driven development (BDD) style. Cucumber defines application behaviour using simple English text, defined by a language called Gherkin, which make tests easy to read and understand for people that don't have much experience with programming. Another advantage of this tests environment is, that it automatically generates report for the conducted test, which might help with analysing tests results. More informations about Cucumber are available in documentation. Testing with cucumberNote: Our project is already prepared to work with Cucumber, so you don't have to re-configure your environment. To make a executable test you have to prepare Feature,Steps Definition and Runnerfiles. In our project Feature file should be located in safetyanalysis/moduleName/src/test/resources/cucumber/FileName.feature, Step Definition file in safetyanalysis/moduleName/src/test/java/steps/FileNameSteps.java and Runner file in safetyanalysis/moduleName/src/test/java/FileName.java Feature filesFeature file includes test scenario description written in Gherkin language. Using basic English language we can describe every single step of our test. It includes feature which is tested, concrete scenarios for this features and test steps. Example of feature file (source):
A feature file consists of 2 main parts:
Every step has to start in new line with one of the keywords:
Using those keywords you can create any test you want. Step definitions fileIt is a file which contains steps definitions. Without him, steps defined in Feature file are just simple words. It contains source code which should be done within a test step. Parameters defined in feature files are passed to step definitions with regular expressions. To make them work correctly, you should configure methods for each step defined in Feature file. Each step is preceded by an annotation connected to step keywords defined in feature file (e.g. @Then )
In this example you can see, how every steps from Feature file (upper part) have to be written in Step definition file (lower part). Even if you make the same method, but written with different words you will have to add another definition to step definition file. This is very important, because every missed step definition make that your test does not be complete, and in consequence it will not end with success. Another thing is, that in Step definition file, there are two steps that are not in Feature file, @Before and @After. Both are used to do things before and after tests. It might be helpful, if we have for example to create class which is require to tests. You can see below more concrete example:
Runner fileIn Runner file you have to set all options about tests, like which features are tested or where steps are located. There's few more options to configure but they are optional, the main thing of this file is that he making our test code working. Required options are:
|
...
:
After completed tests Cucumber automatically generate report file, where you can check what was tested, or if something went wrong where was the problem. Report file is generated as file safetyanalysis/moduleName/target/cucumber/index.html. Scenario outline - alternative scenario typeIn some situations, when we want to test for example results of some mathematic function making the same scenario with different values doesn't make sense. Alternatively you can use Scenario outline in place of normal Scenario to test multiple values at the same scenario. All what you have to do is write tests where instead static values you have to write <variableName>. Later, after describing scenario all you have to do is make after Examples: keyword table, with values for each variant of this scenario. Here you have example of code which use this feature (source):
Example testHere you can see example test of events tree class. Below is code for all three files, which allow to run correctly this test. Feature fileFile located in safetyanalysis/safety-eventstree/src/test/resources/cucumber/EventTree.feature
Step Definition fileFile located in safetyanalysis/safety-eventstree/src/test/java/steps/EventTreeATSteps.java
Runner fileFile located in safetyanalysis/safety-eventstree/src/test/java/EventTreeAcceptanceTest.java
Test Results After run our test we can see in console that it was finished: Below we can see automatically generated report with results of this test. As we can see, everything is green-coloured, which means that test was completed successfully. In other case, red colour will fill all steps that wasn't passed. |