This document discusses strategies for testing code that is difficult or seemingly impossible to test, known as "untestable code". It provides examples of how to address issues like object construction that relies on external resources, dependencies, private methods, and language limitations. Specific techniques include using autoloading, custom stream wrappers, mocking databases/web services, reflection, and generative programming with frames to dynamically generate test and production code. The overall message is that with the right approaches, even legacy or "untestable" code can be made testable.
Unit Testing in Javascript
The way to quality product isn't easy or simple but it is reachable. One of the key things to do is unit testing. What, when and how to do it --> read in the presentation.
See more details here: http://www.slideshare.net/AnnaKhabibullina/jsfwdays-2014unittesingjavascriptv4-33966202
Read about this and other techtalks @ DA-14 in our blog: http://da-14.com/our-blog/
Slides from a tutorial I gave at ETech 2006. Notes to accompany these slides can be found here: http://simonwillison.net/static/2006/js-reintroduction-notes.html
Using Stratego/XT for generation of software connectors.Michal Malohlava
The talk presents a master thesis describing generation of software connectors code in accordance with a high-level connector description and a domain specific language.
Unit Testing in Javascript
The way to quality product isn't easy or simple but it is reachable. One of the key things to do is unit testing. What, when and how to do it --> read in the presentation.
See more details here: http://www.slideshare.net/AnnaKhabibullina/jsfwdays-2014unittesingjavascriptv4-33966202
Read about this and other techtalks @ DA-14 in our blog: http://da-14.com/our-blog/
Slides from a tutorial I gave at ETech 2006. Notes to accompany these slides can be found here: http://simonwillison.net/static/2006/js-reintroduction-notes.html
Using Stratego/XT for generation of software connectors.Michal Malohlava
The talk presents a master thesis describing generation of software connectors code in accordance with a high-level connector description and a domain specific language.
Even nowadays, PHP code is mostly manually audited. Expert pore over actual code, in search for bugs or code smells. Actually, it is possible to have PHP do this work itself ! Strengthened with the internal Tokenizer, bolstered by the manual, it is able to scan thousands of lines of code, without getting bored, and bringing pragmatic pieces of wisdom: official manual recommendations, version migration, code pruning and security. In the end, it deliver a global overview of the code, without reading it.
Coding Naked – Practical steps to moving towards & embracing TDD. We'll overview the normal roadblocks that people typically run in to, and practical ways to overcome those road blocks on your way to embracing Test Driven Development - make coding without tests as uncomfortable as coding naked!
Code: https://github.com/calebjenkins/Acme.CodingNaked
Presenting at events: https://developingux.com/tag/coding-naked/
Unit testing with PHPUnit - there's life outside of TDDPaweł Michalik
Basics of PHPUnit and why is it even more relevant in legacy code. Key points of the framework are explained, along with mocking objects, test organisation, creating documentations with tests and accessing non-public objects (where applicable).
Live version with additional notes available at: http://pawelmichalik.net/presentations/unit-testing-with-phpunit?showNotes=true
Prezentacja dostępna także w języku polskim: http://pawelmichalik.net/prezentacje/testy-jednostkowe-w-phpunit?showNotes=true
This workshop is a hands-on training where a real Zend Framework application is used as an example to start improving QA using tools to test, document and perform software metric calculations to indicate where the software can be improved. I also explain the reports produced by a CI system.
Introduction to functional idioms in Java 8, language-extending functional libraries and short overview of reasons for adoption of such programming style.
Examples and snippets available here: https://github.com/lbialy/functionaljava8
Unit testing is now considered a mainstream practice, but that does not mean it is as common, pervasive or as well understood as it could or should be. Many programmers struggle with the quality of their tests and with the focus of their code. In this session we’ll learn how to write good unit testing code.
2. Testing untestable code
About me
Stephan Hochdörfer, bitExpert AG
Department Manager Research Labs
enjoying PHP since 1999
S.Hochdoerfer@bitExpert.de
@shochdoerfer
14. Testing untestable code
"...our test strategy requires us to
have more control [...] of the sut."
Gerard Meszaros, xUnit Test Patterns: Refactoring Test
Code
26. Testing untestable code
Object construction
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this>Engine = Engine::getByType($sEngine);
}
}
28. Testing untestable code
Object construction
<?php
include('Engine.php');
class Car {
private $Engine;
public function __construct($sEngine) {
$this>Engine = Engine::getByType($sEngine);
}
}
45. Testing untestable code
External resources – Mock filesystem
<?php
// set up test environmemt
vfsStream::setup('exampleDir');
// create directory in test enviroment
mkdir(vfsStream::url('exampleDir').'/sample/');
// check if directory was created
echo vfsStreamWrapper::getRoot()>hasChild('sample');
57. Testing untestable code
Dealing with language issues - Funcall
<?php
function my_func($arg1, $arg2) {
return $arg1.$arg2;
}
function post_cb($args,$result,
$process_time) {
// return custom result based on
$args
}
fc_add_post('my_func','post_cb');
var_dump(my_func('php', 'c'));
60. Testing untestable code
Dealing with language issues - AOP
<?php
aop_add_after('Car::drive*',
'adviceForDrive');
61. Testing untestable code
Dealing with language issues - AOP
<?php
$advice = function(AopTriggeredJoinpoint
$jp) {
$returnValue =
$jp>getReturnedValue();
// modify the return value
$returnValue = 1234;
$jp>setReturnedValue($returnValue);
};
aop_add_after('Car>drive()', $advice);
62. Testing untestable code
And now? Spaghetti mess...
<?php
$all_tables_query = ' SELECT table_name, MAX(version) as
version FROM ...';
$all_tables_result =
PMA_query_as_controluser($all_tables_query);
// If a HEAD version exists
if (PMA_DBI_num_rows($all_tables_result) > 0) {
?>
<div id="tracked_tables">
<h3><?php echo __('Tracked tables');?></h3>
<?php
}
69. Testing untestable code
Generative Programming
A frame is a data structure
for representing knowledge.
70. Testing untestable code
Frame
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this>Engine = <!{Factory}!>::
getByType($sEngine);
}
}
71. Testing untestable code
ContentProvider for the Frame
public class MyContentProvider extends
AbstractContentProvider {
public SlotConfiguration computeSlots(
FeatureConfiguration config) {
SlotConfiguration sl = new SlotConfiguration();
if(config.hasFeature("unittest")) {
sl.put("Factory", "FactoryMock");
} else {
sl.put("Factory", "EngineFactory");
}
return sl;
}
}
72. Testing untestable code
Generated result – Test Enviroment
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this>Engine = FactoryMock::
getByType($sEngine);
}
}
73. Testing untestable code
Generated result – Prod. Enviroment
<?php
class Car {
private $Engine;
public function __construct($sEngine) {
$this>Engine = EngineFactory::
getByType($sEngine);
}
}