[Cuis-dev] Tests with random

Luciano Notarfrancesco luchiano at gmail.com
Wed Mar 24 07:42:44 PDT 2021


Hi,
I'd like to propose a change for TestCase that allows reproducible tests
that use randomness. The original idea is from Leandro Caniglia, he
implemented it in Bee Smalltalk. It works like this: every TestCase has a
'seed' instance variable that is set at random during initialization, and
any test can send the message 'random' to get a new instance of Random
initialized with this seed. This makes tests with randomness reproducible,
i.e. if a test fails or produces an error and you want to debug it, it will
be run again with the same seed as the first time.

In order to make this work I had to also change the methods TestCase>>debug
and TestCase>>debugAsFailureIfCanNot:, because instead of running again the
same instance of TestCase they created a new one. Could someone more
familiar with SUnit look at this and let me know if it's ok? It doesn't
seem necessary to create a new instance of the TestCase every time you run
it, but I might be missing something. If for some reason it is important to
create a fresh instance we could copy it so it keeps the seed and any other
state that was set at initialization. Keeping the state is important for my
tests, not only due to the use of randomness but also because I'm doing
parametrized testing (for example I generic tests for vector spaces and I
set a particular coefficients field, if the TestCase is recreated sending
TestCase class>>selector: the extra information of the coefficients field
is lost).

Thanks,
Luciano
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20210324/e8fb9b93/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 4543-TestsWithRandom-LucianoEstebanNotarfrancesco-2021Mar24-12h49m-len.001.cs.st
Type: application/octet-stream
Size: 2673 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20210324/e8fb9b93/attachment.obj>


More information about the Cuis-dev mailing list