Best Atoum code snippet using xunit.build
XUnitTestEngine.php
Source:XUnitTestEngine.php
...9 * @concrete-extensible10 */11class XUnitTestEngine extends ArcanistUnitTestEngine {12 protected $runtimeEngine;13 protected $buildEngine;14 protected $testEngine;15 protected $projectRoot;16 protected $xunitHintPath;17 protected $discoveryRules;18 /**19 * This test engine supports running all tests.20 */21 protected function supportsRunAllTests() {22 return true;23 }24 /**25 * Determines what executables and test paths to use. Between platforms this26 * also changes whether the test engine is run under .NET or Mono. It also27 * ensures that all of the required binaries are available for the tests to28 * run successfully.29 *30 * @return void31 */32 protected function loadEnvironment() {33 $this->projectRoot = $this->getWorkingCopy()->getProjectRoot();34 // Determine build engine.35 if (Filesystem::binaryExists('msbuild')) {36 $this->buildEngine = 'msbuild';37 } else if (Filesystem::binaryExists('xbuild')) {38 $this->buildEngine = 'xbuild';39 } else {40 throw new Exception(41 pht(42 'Unable to find %s or %s in %s!',43 'msbuild',44 'xbuild',45 'PATH'));46 }47 // Determine runtime engine (.NET or Mono).48 if (phutil_is_windows()) {49 $this->runtimeEngine = '';50 } else if (Filesystem::binaryExists('mono')) {51 $this->runtimeEngine = Filesystem::resolveBinary('mono');52 } else {53 throw new Exception(54 pht('Unable to find Mono and you are not on Windows!'));55 }56 // Read the discovery rules.57 $this->discoveryRules =58 $this->getConfigurationManager()->getConfigFromAnySource(59 'unit.csharp.discovery');60 if ($this->discoveryRules === null) {61 throw new Exception(62 pht(63 'You must configure discovery rules to map C# files '.64 'back to test projects (`%s` in %s).',65 'unit.csharp.discovery',66 '.arcconfig'));67 }68 // Determine xUnit test runner path.69 if ($this->xunitHintPath === null) {70 $this->xunitHintPath =71 $this->getConfigurationManager()->getConfigFromAnySource(72 'unit.csharp.xunit.binary');73 }74 $xunit = $this->projectRoot.DIRECTORY_SEPARATOR.$this->xunitHintPath;75 if (file_exists($xunit) && $this->xunitHintPath !== null) {76 $this->testEngine = Filesystem::resolvePath($xunit);77 } else if (Filesystem::binaryExists('xunit.console.clr4.exe')) {78 $this->testEngine = 'xunit.console.clr4.exe';79 } else {80 throw new Exception(81 pht(82 "Unable to locate xUnit console runner. Configure ".83 "it with the `%s' option in %s.",84 'unit.csharp.xunit.binary',85 '.arcconfig'));86 }87 }88 /**89 * Main entry point for the test engine. Determines what assemblies to build90 * and test based on the files that have changed.91 *92 * @return array Array of test results.93 */94 public function run() {95 $this->loadEnvironment();96 if ($this->getRunAllTests()) {97 $paths = id(new FileFinder($this->projectRoot))->find();98 } else {99 $paths = $this->getPaths();100 }101 return $this->runAllTests($this->mapPathsToResults($paths));102 }103 /**104 * Applies the discovery rules to the set of paths specified.105 *106 * @param array Array of paths.107 * @return array Array of paths to test projects and assemblies.108 */109 public function mapPathsToResults(array $paths) {110 $results = array();111 foreach ($this->discoveryRules as $regex => $targets) {112 $regex = str_replace('/', '\\/', $regex);113 foreach ($paths as $path) {114 if (preg_match('/'.$regex.'/', $path) === 1) {115 foreach ($targets as $target) {116 // Index 0 is the test project (.csproj file)117 // Index 1 is the output assembly (.dll file)118 $project = preg_replace('/'.$regex.'/', $target[0], $path);119 $project = $this->projectRoot.DIRECTORY_SEPARATOR.$project;120 $assembly = preg_replace('/'.$regex.'/', $target[1], $path);121 $assembly = $this->projectRoot.DIRECTORY_SEPARATOR.$assembly;122 if (file_exists($project)) {123 $project = Filesystem::resolvePath($project);124 $assembly = Filesystem::resolvePath($assembly);125 // Check to ensure uniqueness.126 $exists = false;127 foreach ($results as $existing) {128 if ($existing['assembly'] === $assembly) {129 $exists = true;130 break;131 }132 }133 if (!$exists) {134 $results[] = array(135 'project' => $project,136 'assembly' => $assembly,137 );138 }139 }140 }141 }142 }143 }144 return $results;145 }146 /**147 * Builds and runs the specified test assemblies.148 *149 * @param array Array of paths to test project files.150 * @return array Array of test results.151 */152 public function runAllTests(array $test_projects) {153 if (empty($test_projects)) {154 return array();155 }156 $results = array();157 $results[] = $this->generateProjects();158 if ($this->resultsContainFailures($results)) {159 return array_mergev($results);160 }161 $results[] = $this->buildProjects($test_projects);162 if ($this->resultsContainFailures($results)) {163 return array_mergev($results);164 }165 $results[] = $this->testAssemblies($test_projects);166 return array_mergev($results);167 }168 /**169 * Determine whether or not a current set of results contains any failures.170 * This is needed since we build the assemblies as part of the unit tests, but171 * we can't run any of the unit tests if the build fails.172 *173 * @param array Array of results to check.174 * @return bool If there are any failures in the results.175 */176 private function resultsContainFailures(array $results) {177 $results = array_mergev($results);178 foreach ($results as $result) {179 if ($result->getResult() != ArcanistUnitTestResult::RESULT_PASS) {180 return true;181 }182 }183 return false;184 }185 /**186 * If the `Build` directory exists, we assume that this is a multi-platform187 * project that requires generation of C# project files. Because we want to188 * test that the generation and subsequent build is whole, we need to189 * regenerate any projects in case the developer has added files through an190 * IDE and then forgotten to add them to the respective `.definitions` file.191 * By regenerating the projects we ensure that any missing definition entries192 * will cause the build to fail.193 *194 * @return array Array of test results.195 */196 private function generateProjects() {197 // No "Build" directory; so skip generation of projects.198 if (!is_dir(Filesystem::resolvePath($this->projectRoot.'/Build'))) {199 return array();200 }201 // No "Protobuild.exe" file; so skip generation of projects.202 if (!is_file(Filesystem::resolvePath(203 $this->projectRoot.'/Protobuild.exe'))) {204 return array();205 }206 // Work out what platform the user is building for already.207 $platform = phutil_is_windows() ? 'Windows' : 'Linux';208 $files = Filesystem::listDirectory($this->projectRoot);209 foreach ($files as $file) {210 if (strtolower(substr($file, -4)) == '.sln') {211 $parts = explode('.', $file);212 $platform = $parts[count($parts) - 2];213 break;214 }215 }216 $regenerate_start = microtime(true);217 $regenerate_future = new ExecFuture(218 '%C Protobuild.exe --resync %s',219 $this->runtimeEngine,220 $platform);221 $regenerate_future->setCWD(Filesystem::resolvePath(222 $this->projectRoot));223 $results = array();224 $result = new ArcanistUnitTestResult();225 $result->setName(pht('(regenerate projects for %s)', $platform));226 try {227 $regenerate_future->resolvex();228 $result->setResult(ArcanistUnitTestResult::RESULT_PASS);229 } catch (CommandException $exc) {230 if ($exc->getError() > 1) {231 throw $exc;232 }233 $result->setResult(ArcanistUnitTestResult::RESULT_FAIL);234 $result->setUserData($exc->getStdout());235 }236 $result->setDuration(microtime(true) - $regenerate_start);237 $results[] = $result;238 return $results;239 }240 /**241 * Build the projects relevant for the specified test assemblies and return242 * the results of the builds as test results. This build also passes the243 * "SkipTestsOnBuild" parameter when building the projects, so that MSBuild244 * conditionals can be used to prevent any tests running as part of the245 * build itself (since the unit tester is about to run each of the tests246 * individually).247 *248 * @param array Array of test assemblies.249 * @return array Array of test results.250 */251 private function buildProjects(array $test_assemblies) {252 $build_futures = array();253 $build_failed = false;254 $build_start = microtime(true);255 $results = array();256 foreach ($test_assemblies as $test_assembly) {257 $build_future = new ExecFuture(258 '%C %s',259 $this->buildEngine,260 '/p:SkipTestsOnBuild=True');261 $build_future->setCWD(Filesystem::resolvePath(262 dirname($test_assembly['project'])));263 $build_futures[$test_assembly['project']] = $build_future;264 }265 $iterator = id(new FutureIterator($build_futures))->limit(1);266 foreach ($iterator as $test_assembly => $future) {267 $result = new ArcanistUnitTestResult();268 $result->setName('(build) '.$test_assembly);269 try {270 $future->resolvex();271 $result->setResult(ArcanistUnitTestResult::RESULT_PASS);272 } catch (CommandException $exc) {273 if ($exc->getError() > 1) {274 throw $exc;275 }276 $result->setResult(ArcanistUnitTestResult::RESULT_FAIL);277 $result->setUserData($exc->getStdout());278 $build_failed = true;279 }280 $result->setDuration(microtime(true) - $build_start);281 $results[] = $result;282 }283 return $results;284 }285 /**286 * Build the future for running a unit test. This can be overridden to enable287 * support for code coverage via another tool.288 *289 * @param string Name of the test assembly.290 * @return array The future, output filename and coverage filename291 * stored in an array.292 */293 protected function buildTestFuture($test_assembly) {294 // FIXME: Can't use TempFile here as xUnit doesn't like295 // UNIX-style full paths. It sees the leading / as the296 // start of an option flag, even when quoted.297 $xunit_temp = Filesystem::readRandomCharacters(10).'.results.xml';298 if (file_exists($xunit_temp)) {299 unlink($xunit_temp);300 }301 $future = new ExecFuture(302 '%C %s /xml %s',303 trim($this->runtimeEngine.' '.$this->testEngine),304 $test_assembly,305 $xunit_temp);306 $folder = Filesystem::resolvePath($this->projectRoot);307 $future->setCWD($folder);308 $combined = $folder.'/'.$xunit_temp;309 if (phutil_is_windows()) {310 $combined = $folder.'\\'.$xunit_temp;311 }312 return array($future, $combined, null);313 }314 /**315 * Run the xUnit test runner on each of the assemblies and parse the316 * resulting XML.317 *318 * @param array Array of test assemblies.319 * @return array Array of test results.320 */321 private function testAssemblies(array $test_assemblies) {322 $results = array();323 // Build the futures for running the tests.324 $futures = array();325 $outputs = array();326 $coverages = array();327 foreach ($test_assemblies as $test_assembly) {328 list($future_r, $xunit_temp, $coverage) =329 $this->buildTestFuture($test_assembly['assembly']);330 $futures[$test_assembly['assembly']] = $future_r;331 $outputs[$test_assembly['assembly']] = $xunit_temp;332 $coverages[$test_assembly['assembly']] = $coverage;333 }334 // Run all of the tests.335 $futures = id(new FutureIterator($futures))336 ->limit(8);337 foreach ($futures as $test_assembly => $future) {338 list($err, $stdout, $stderr) = $future->resolve();339 if (file_exists($outputs[$test_assembly])) {340 $result = $this->parseTestResult(341 $outputs[$test_assembly],342 $coverages[$test_assembly]);343 $results[] = $result;344 unlink($outputs[$test_assembly]);345 } else {346 // FIXME: There's a bug in Mono which causes a segmentation fault347 // when xUnit.NET runs; this causes the XML file to not appear348 // (depending on when the segmentation fault occurs). See349 // https://bugzilla.xamarin.com/show_bug.cgi?id=16379350 // for more information.351 // Since it's not possible for the user to correct this error, we352 // ignore the fact the tests didn't run here.353 }354 }355 return array_mergev($results);356 }357 /**358 * Returns null for this implementation as xUnit does not support code359 * coverage directly. Override this method in another class to provide code360 * coverage information (also see @{class:CSharpToolsUnitEngine}).361 *362 * @param string The name of the coverage file if one was provided by363 * `buildTestFuture`.364 * @return array Code coverage results, or null.365 */366 protected function parseCoverageResult($coverage) {367 return null;368 }369 /**370 * Parses the test results from xUnit.371 *372 * @param string The name of the xUnit results file.373 * @param string The name of the coverage file if one was provided by374 * `buildTestFuture`. This is passed through to375 * `parseCoverageResult`.376 * @return array Test results.377 */378 private function parseTestResult($xunit_tmp, $coverage) {379 $xunit_dom = new DOMDocument();380 $xunit_dom->loadXML(Filesystem::readFile($xunit_tmp));381 $results = array();382 $tests = $xunit_dom->getElementsByTagName('test');383 foreach ($tests as $test) {384 $name = $test->getAttribute('name');385 $time = $test->getAttribute('time');386 $status = ArcanistUnitTestResult::RESULT_UNSOUND;387 switch ($test->getAttribute('result')) {388 case 'Pass':...
build
Using AI Code Generation
1$test = new xunit();2$test->build();3$test = new xunit();4$test->build();5$test = new xunit();6$test->build();7$test = new xunit();8$test->build();9$test = new xunit();10$test->build();11$test = new xunit();12$test->build();13$test = new xunit();14$test->build();15$test = new xunit();16$test->build();17$test = new xunit();18$test->build();19$test = new xunit();20$test->build();21$test = new xunit();22$test->build();23$test = new xunit();24$test->build();25$test = new xunit();26$test->build();27$test = new xunit();28$test->build();29$test = new xunit();30$test->build();31$test = new xunit();32$test->build();33$test = new xunit();34$test->build();
build
Using AI Code Generation
1$test = new xunit();2$test->build('test');3$test->run();4$test = new xunit();5$test->build('test2');6$test->run();7The xunit class has a method called run() which is
build
Using AI Code Generation
1$test = new xunit();2$test->build('1.php');3$test->run();4$test->report();5$test = new xunit();6$test->build('2.php');7$test->run();8$test->report();9$test = new xunit();10$test->build('3.php');11$test->run();12$test->report();13$test = new xunit();14$test->build('4.php');15$test->run();16$test->report();17$test = new xunit();18$test->build('5.php');19$test->run();20$test->report();21$test = new xunit();22$test->build('6.php');23$test->run();24$test->report();25$test = new xunit();26$test->build('7.php');27$test->run();28$test->report();29$test = new xunit();30$test->build('8.php');31$test->run();32$test->report();33$test = new xunit();34$test->build('9.php');35$test->run();36$test->report();37$test = new xunit();38$test->build('10.php');39$test->run();40$test->report();41$test = new xunit();42$test->build('11.php');43$test->run();44$test->report();
build
Using AI Code Generation
1require_once 'xunit.class.php';2$xunit = new xunit;3$xunit->addTestCase('test1');4$xunit->addTestCase('test2');5$xunit->build('test.xml');6require_once 'xunit.class.php';7$xunit = new xunit;8$xunit->addTestCase('test3');9$xunit->addTestCase('test4');10$xunit->build('test.xml');
build
Using AI Code Generation
1require_once 'PHPUnit/Autoload.php';2require_once 'PHPUnit/Util/Filter.php';3PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');4{5 public static function suite()6 {7 $suite = new MyTestSuite('MyTestSuite');8 $suite->addTestSuite('MyTest');9 return $suite;10 }11}12{13 public function testOnePlusOne()14 {15 $this->assertEquals(1+1, 2);16 }17}18require_once 'PHPUnit/Autoload.php';19require_once 'PHPUnit/Util/Filter.php';20PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');21{22 public static function suite()23 {24 $suite = new MyTestSuite('MyTestSuite');25 $suite->addTestSuite('MyTest');26 return $suite;27 }28}29{30 public function testOnePlusOne()31 {32 $this->assertEquals(1+1, 2);33 }34}35require_once 'PHPUnit/Autoload.php';36require_once 'PHPUnit/Util/Filter.php';37PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');38{39 public static function suite()40 {41 $suite = new MyTestSuite('MyTestSuite');42 $suite->addTestSuite('MyTest');43 return $suite;44 }45}46{47 public function testOnePlusOne()48 {49 $this->assertEquals(1+1, 2);50 }51}52require_once 'PHPUnit/Autoload.php';53require_once 'PHPUnit/Util/Filter.php';54PHPUnit_Util_Filter::addFileToFilter(__
build
Using AI Code Generation
1$test = new xunit();2$test->setTestName('test');3$test->setTestSuite('testsuite');4$test->setTestCase('testcase');5$test->setTestDescription('testdesc');6$test->setTestError('testerror');7$test->setTestFailure('testfailure');8$test->setTestSkipped('testskipped');9$test->setTestTime('testtime');10$test->setTestType('testtype');11$test->setTestFile('testfile');12$test->setTestLine('testline');13$test->setTestClass('testclass');14$test->setTestFunction('testfunction');15$test->build();16$test = new xunit();17$test->setTestName('test');18$test->setTestSuite('testsuite');19$test->setTestCase('testcase');20$test->setTestDescription('testdesc');21$test->setTestError('testerror');22$test->setTestFailure('testfailure');23$test->setTestSkipped('testskipped');24$test->setTestTime('testtime');25$test->setTestType('testtype');26$test->setTestFile('testfile');27$test->setTestLine('testline');28$test->setTestClass('testclass');29$test->setTestFunction('testfunction');30$test->build();31$test = new xunit();32$test->setTestName('test');33$test->setTestSuite('testsuite');34$test->setTestCase('testcase');35$test->setTestDescription('testdesc');36$test->setTestError('testerror');37$test->setTestFailure('testfailure');38$test->setTestSkipped('testskipped');39$test->setTestTime('testtime');40$test->setTestType('testtype');41$test->setTestFile('testfile');42$test->setTestLine('testline');43$test->setTestClass('testclass');44$test->setTestFunction('testfunction');45$test->build();
build
Using AI Code Generation
1require_once('xunit.php');2$test = new xunit('test');3$test->build();4require_once('xunit.php');5$test = new xunit('test');6$test->assert(1==1, '1 is equal to 1');7$test->assert(1==2, '1 is not equal to 2');8$test->build();
build
Using AI Code Generation
1require_once('xunit.php');2class TestSuite1 extends TestSuite {3 function TestSuite1() {4 $this->TestSuite('TestSuite1');5 }6 function setUp() {7 echo "TestSuite1::setUp()";8 }9 function tearDown() {10 echo "TestSuite1::tearDown()";11 }12}13class TestSuite2 extends TestSuite {14 function TestSuite2() {15 $this->TestSuite('TestSuite2');16 }17 function setUp() {18 echo "TestSuite2::setUp()";19 }20 function tearDown() {21 echo "TestSuite2::tearDown()";22 }23}24class Test1 extends TestCase {25 function Test1($name) {26 $this->TestCase($name);27 }28 function setUp() {29 echo "Test1::setUp()";30 }31 function tearDown() {32 echo "Test1::tearDown()";33 }34 function test1() {35 echo "Test1::test1()";36 }37 function test2() {38 echo "Test1::test2()";39 }40}41class Test2 extends TestCase {42 function Test2($name) {43 $this->TestCase($name);44 }45 function setUp() {46 echo "Test2::setUp()";47 }48 function tearDown() {49 echo "Test2::tearDown()";50 }51 function test1() {52 echo "Test2::test1()";53 }54 function test2() {55 echo "Test2::test2()";56 }57}58$test_suite1 = new TestSuite1;59$test_suite1->addTest(new Test1('test1'));60$test_suite1->addTest(new Test1('test2'));61$test_suite2 = new TestSuite2;62$test_suite2->addTest(new Test2('test1'));63$test_suite2->addTest(new Test2('test2'));64$test_suite = new TestSuite;65$test_suite->addTest($test_suite1);66$test_suite->addTest($test_suite2);67$test_result = new TestResult;68$test_suite->run($test_result);69echo $test_result->toString();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with build on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!