[TYPO3-dev] Questions about unit and functional tests

Charles Brunet charles.fmj at gmail.com
Mon Feb 23 17:17:43 CET 2015


Hello,

I began writing my first unit and functional tests for a TYPO3 extension. I
realized that there is very few documentation available about this topic,
although extbase development is supposed to be "test-driven". There are few
hints on the wiki page [1], but I finally found most of my answers reading
though TYPO3 and other extension source code. Even the manual about writing
extbase extensions is missing code samples [2].

One thing I realized is that the most relevant classes used for writing
tests, BaseTestCase, UnitTestCase, FunctionalTestCase, and other related
classes, located in EXT:core/Tests, are not documented in the api generated
documentation [3]. Is it something intentional? It took me a while before
figuring out how to do some basic things, like loading TypoScript setup in
a functional test.

Another thing I realized is that some globals, like the database
connection, are set when running the tests from the phpunit module, but not
when running them from the command line using the bootstrap script. I
should not need the database in a unit test. However, the function I needed
to test is using xml2array function. The problem is that xml2array is
caching the results, and therefore needs a lot of things to be setup to be
able to run. If I run my tests from the phpunit module, everything runs
fine. But if I want to run them from the command line, I need to mock
database connection and cacheFrontend, otherwise the xml2array function
fails. My question is: am I doing it the right way? Is there an easier way
to prevent xml2array to try reading and writing caches in unit tests?

The extension I'm developing can optionally use another extension. In unit
tests, I test whether this optional extension is loaded before executing
some tests, otherwise I skip the test. However, in functional tests, I need
to specify the extensions that need to be loaded as class attributes of the
test class. My problem is: if I put the optional extension in the
$testExtensionsToLoad array, the tests will fail if the optional extension
is not available; if I do not put it there, database schema and other
required parts of that extension will not be available for testing. What
should I do in that case? Should I include the needed parts of the optional
extension into the Fixture directory to ensure it is always available?
However, if I do that, the tests will not detect a problem if something in
the optional extension change.


Thank you for your support. As a final word, I'd like to say that I learned
a lot of new things about how TYPO3 and extbase work, by writing tests, and
figuring out what needed to be mocked, and how it should be done.



[1] http://wiki.typo3.org/Category:PHPUnit
[2]
http://docs.typo3.org/typo3cms/ExtbaseFluidBook/2-BasicPrinciples/4-Test-Driven-Development.html
[3] http://typo3.org/api/typo3cms/index.html

*--*
*Charles Brunet, B. Ing.*
Webmestre.
Étudiant au doctorat, génie électrique, Université Laval.



More information about the TYPO3-dev mailing list