OBJECTSEMANTICS / SmallSuiteGenerator

Powered by SEMANTICS SRL
http://semantics.bo
MIT License
3 stars 2 forks source link
genetic-algorithms pharo testcases

SmallSuiteGenerator

SmallSuiteGenerator is an Smalltalk tool to generate test cases automatically

master branch:

MIT Licensed.

SmallSuiteGenerator can be used to discover bugs in the code, because it generates automatically test cases

1. Installation

1.1. Pharo

To install the latest stable version of SmallSuiteGenerator in a Pharo image, execute the following code:

Metacello new
 baseline:'SmallSuiteGenerator';
 repository: 'github://OBJECTSEMANTICS/SmallSuiteGenerator:master/src';
 load.

To install the version that presents graphic results of evolution coverage, execute the following code:

Metacello new
 baseline:'SmallSuiteGenerator';
 repository: 'github://OBJECTSEMANTICS/SmallSuiteGenerator:master/src';
 load: #('All').

To install the version that allows SmallSuiteGenerator to be exported to VisualWorks, run the following code:

Metacello new
 baseline:'SmallSuiteGenerator';
 repository: 'github://OBJECTSEMANTICS/SmallSuiteGenerator:master/src';
 load: #('Exporter').

1.2. VisualWorks

See documentation in Install FuzzyTester

2. Configuration

There are two ways to do the automatic generation of tests, in the first way we use the automatic generation of this class, and in the second we define the configuration in a class generated by ourselves.

Define the typeInfo. You must first define the block of code that will be instrumented to get the typeInfo. Also define the regular expression pattern for the package.

| typeInfo aBlock |
aBlock := [ 
            (SSTeacher name: 'Ann' with: 50)
            nickname;
            canRegister: ((SConference price: 50) offerPrice: 50);
            idTeacher;
            yearsWorkExperience ].
typeInfo := STypeInfo asTypeInfo: (
        SSTypeCollector profile: aBlock onPackagesMatching: 'SmallSuiteGenerator-Scenario').

2.1. Generate tests from default

The STestCaseFactor class allows you to create a default test class, for this you must first provide the name of the targetClass, here is an example of this.

(STestCaseFactoryPharo new)
    targetClassName: #SSTeacher;
    typeInfo: typeInfo;
    seedBlock: aBlock;
    createTestCases;
    yourself. 

You can also modify some default values for the generation of tests, an example is presented below.

(STestCaseFactoryPharo new)
    targetClassName: #SSTeacher;
    typeInfo: typeInfo;
    targetPackageRegex: 'SmallSuiteGenerator-Scenario';
    outputPackageName: 'Generated';
    numberOfGenerations: 10;
    numberOfStatements: 30;
    populationSize: 20;
    seedBlock: aBlock;
    createTestCases;
    yourself.

After to execute this script, the testCases will be generated with the higher fitness of the population. In the output package name specified you can find the generated testCases. Usually the last enumerated testCases have the highests fitness.

In case you have loaded the version with visualization and you want to generate the evolution graphs, execute the generateViews method beforecreateTestCases.

Hint: The methods to visualize only are available if you install the version with graphics, specified before.

2.2. Generate tests from a configuration class

  1. Define configuration class. To do this, you must create the configuration class for the class you want to generate tests, to do this, execute the following command with the class you want, in this case we will create the configuration class for "SSTeacher" class.
SSTeacher createTestCaseConfig

With the script above, a class with the name "GASSTeacherTest" is automatically created in "GeneratedTests" package, which contains a series of default settings that inherits from "SConfigGenerationTestCase" superclass.

In case you prefer to generate a configuration class with a predefined name, you can run the following script:

SSTeacher createTestCaseConfigWithName: 'SSTeacherTest'
  1. Change the default settings to your preference. Here are the most common settings that can be modified:
(STestCaseFactoryPharo from: GASSTeacherTest) 
    setUpMethod: 'setUp
    ^ self';
    tearDownMethod: 'tearDown
    ^ self'
GASSTeacherTest fitness: #statement
  1. Save and assign the typeInfo. Using the previously defined typeInfo, you can save this typeInfo in the configuration class, this in order to easily reuse the typeInfo and avoid generating it each time a different experiment is performed. To save typeInfo, use the following code
GASSTeacherTest addTypeInfo: typeInfo withKey: 'steacher'

Now, you need to reference the typeInfo by means of its key in the configuration class. Finally, we will assign the typeInfo that we save.

GASSTeacherTest typeInfo: 'steacher'
  1. Generate tests. Below is an example of how to generate tests from this configuration class.
(STestCaseFactoryPharo from: GASSTeacherTest)
    seedBlock: aBlock;
    createTestCases;
    yourself. 

Advanced settings

All these settings should be done before generate test cases

GASSTeacherTest asDict: true.

Examples

|typeInfo aBlock generated |
aBlock := [SFoo new
                return: Dictionary new;
                return: OrderedCollection new;
                return: (SSTeacher name: 'Ana' with: 11);
                returnFloat;
                returnString;
                returnCollection;
                returnNum;
                score;
                score: 5].
typeInfo := STypeInfo asTypeInfo: (
        SSTypeCollector profile: aBlock onPackagesMatching: 'SmallSuiteGenerator-Scenario').

generated := SStack createTestCaseConfigWithName: 'SSFooTestGenerated'.

generated addTypeInfo: typeInfo withKey: 'sfoo';
    typeInfo: 'sfoo';
    fitness: #statement;
    numberOfGenerations: 10;
    numberOfStatements: 30;
    populationSize: 20;
    stopIterations: 5;
    targetPackageRegex: 'SmallSuiteGenerator-Scenario';
    outputPackageName: 'Generated'.

(STestCaseFactoryPharo from: SSFooTestGenerated )
    generateViews;
    seedBlock: aBlock;
    setUpMethod: 'setUp
    ^ self';
    tearDownMethod: 'tearDown
    ^ self';
    createTestCases;
    yourself.
|typeInfo aBlock generated |
aBlock := [SFoo new
                return: Dictionary new;
                return: OrderedCollection new;
                return: (SSTeacher name: 'Ana' with: 11);
                returnFloat;
                returnString;
                returnCollection;
                returnNum;
                score;
                score: 5].
typeInfo := STypeInfo asTypeInfo: (
        SSTypeCollector profile: aBlock onPackagesMatching: 'SmallSuiteGenerator-Scenario').

(STestCaseFactoryPharo new)
    targetClassName: #SFoo;
    typeInfo: typeInfo;
    targetPackageRegex: 'SmallSuiteGenerator-Scenario';
    outputPackageName: 'Generated';
    numberOfGenerations: 10;
    numberOfStatements: 30;
    populationSize: 20;
    seedBlock: aBlock;
    generateViews;
    createTestCases;
    yourself. 

Install SpyLite

See documentation in Install SpyLite

Install TestRunner

See documentation in Install TestRunner