You can implement step definitions for undefined steps with these snippets:
/**
* #Given /^people enter (\d+)$/
*/
public function peopleEnter($argument1)
{
throw new Pending();
}
Should I put it under bootstrap.php? I'm really confused what should I do know...
I want to use the oop style, not closures.
I'm new to BDD and Behat.
Any help is welcome.
Thanks
Yes, put it in the Bootstrap. By default, Behat looks for bootstrap/FeatureContext.php as its bootstrap file, but these methods should go into whatever bootstrap class you're using that extends BehatContext.
Related
I'm fairly new to using PHPUnit and I'm trying to use it more effectively by using data providers. I can get data providers to work when writing a normal test case, however I find that I'm rewriting my setup code for across several testcases. So I'm trying to extend PHPUnit_Framework_TestCase with a BaseTestCase class that does all of my common setup. This works if I run simple test in a test case that extends my BaseTestCase class. However I can't seem to use #dataProvider when extending my BaseTestCase class.
So my setup is:
class BaseTestCase extends PHPUnit_Framework_TestCase{
public static function setUpBeforeClass(){
//do setup
//this is getting called and it works
}
}
class myTest extends BaseTestCase{
public function myBasicTest(){
//this works
$this->assertEquals(2, 1+1);
}
public function myProvider(){
return [
[1,1,2],
[1,2,3],
[1,4,5],
]
}
/**
* #dataProvider myProvider
*/
public function testMyProvider($a, $b, $result){
//this doesn't work, the provider never gets called
//this would normally work if I extended PHPUnit_Framework_TestCase
$this->assertEquals($result, $a+$b);
}
}
I know the providers get ran before any of the setup does so I'm wondering if PHPUnit doesn't know that the provider exists because of the inheritance. Either way, does anyone know if what I'm trying to do is possible this way or does PHPUnit have another way of accommodating these types of situations?
Thanks for your help,
Jordan
Your test function does not begin with the word 'test'.
public function test_myProviderTest($a, $b, $result){
This is actually a non issue. I had an incorrect constructor setup in my test file. A very frustrating oversight.
I'm new to unit testing so this may be a daft question.
Usinf mvc 4 I have a view which was working fine.
I declared my model at the top and all was fine.
I then extracted my models out into a seperate library and forgot to change the model declaration on one of my views, hence is crashes.
Is there a way to unit test this?
I don't want to do it by the page title of the view as this may change dynamically...
How is this handled normally or is this something not normally tested?
I have a series of tests I run against each View Model, including a test to make sure the expected property names are there. Here is an example:
/// <summary>
/// Check expected properties exist.
/// </summary>
[Test]
public void Check_Expected_Properties_Exist()
{
// Get properties.
PropertyInfo propInfoFirstName = typeof(ViewModels.MyModel).GetProperty("FirstName");
PropertyInfo propInfoLastName = typeof(ViewModels.MyModel).GetProperty("LastName");
// Assert.
Assert.IsNotNull(propInfoFirstName);
Assert.IsNotNull(propInfoLastName);
}
This is just one of a number of test I run, I will write a blog article on this topic and update this answer when it's ready.
I have added another Answer, in case the previous answer is helpful to anyone else.
Take a look at Selnium WebDriver or WatiN. There are plenty of tutorials and how-tos to get you started.
I usually add a very simple test for each method in my controller to check it return a valid ActionResult.
[TestMethod]
public void TestMyController()
{
// Arrange.
var controller = new MyController();
// Act.
var result = controller.MyMethod() as ViewResult;
// Assert.
Assert.IsNotNull(result);
}
I also add a few tests for each View Model, as I have had an issue with these when they were in a separate class library, but this is outside the scope of your question.
A lot of my tests have a lot of the same setUp()/tearDown() stuff going on. It seems dumb to copy and paste the same code into every single one of my unit tests. I think I want to create a new test class that extends WebTestCase that my other tests can extend.
My problem is that I don't really know how. First of all, where's the most appropriate place to put this new class? I tried making one just inside my Tests folder, but then none of my tests could actually find the class. Maybe I just don't understand namespaces.
Has anyone extended WebTestCase before in the way I'm talking about? If so, how did you do it?
I haven't done this, but I'd probably just do it like so
src/Your/Bundle/Test/WebTestCase.php
<?php
namespace Your\Bundle\Test
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as WTC;
class WebTestCase extends WTC
{
// Your implementation of WebTestCase
}
In my tests I usually extend the WebTestCase the way Peter proposed it. Additionally I use require_once to make the AppKernel available in my WebTestCase:
<?php
namespace My\Bundle\Tests;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
require_once(__DIR__ . "/../../../../app/AppKernel.php");
class WebTestCase extends BaseWebTestCase
{
protected $_application;
protected $_container;
public function setUp()
{
$kernel = new \AppKernel("test", true);
$kernel->boot();
$this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$this->_application->setAutoExit(false);
...
My tests then look like this:
<?php
namespace My\Bundle\Tests\Controller;
use My\Bundle\Tests\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
...
You don't need to extend WebTestCase to include AppKernel. You can use the following approach
$client = static::createClient();
self::$application = new Application($client->getKernel());
This question is really old but it ranks quite highly on Google, so I thought I'd add my solution.
I extended WebTestCase with a default setUp method and a logIn method so I could more easily run authenticated tests.
It seems you can't add standard classes to the tests directory because the unit tests won't find them. I'm not sure why. My solution was to add a directory src/_TestHelpers and put my helper classes in there.
So I have:
# src/_TestHelpers/ExtendedWTC.php
namespace _TestHelpers;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ExtendedWTC extends WebTestCase
{
# Do your cool extending here
}
and
# tests/AppBundle/Controller/DefaultControllerTest.php
namespace Tests\AppBundle;
use _TestHelpers\ExtendedWTC
class DefaultControllerTest extends ExtendedWTC
{
# Your tests here, using your cool extensions.
}
Note: I'm using the Symfony 3 directory structure, hence why my tests are in tests/ and not src/AppBundle/Tests/.
I hope this helps someone...
Has anyone had any lucking creating their custom test class and using the IProvideDynamicTestMethods interface? I have a case where I need to dynamically generate test methods and this seems to be what I need. I goal is to have test methods generated based on some files I am testing.
Jeff Wilcox mentions this as a new feature in SL3 ( http://www.jeff.wilcox.name/2008/09/rc0-new-test-features/ , see the dynamic test methods section), but I was unable to find any examples of this.
I don't know how to register my custom test class (it inherits from ITestClass). I looked at the SL4 unit testing source to see how the test class are discovered and I found the following in UnitTestFrameworkAssembly.cs source link
/// <summary>
/// Reflect and retrieve the test class metadata wrappers for
/// the test assembly.
/// </summary>
/// <returns>Returns a collection of test class metadata
/// interface objects.</returns>
public ICollection<ITestClass> GetTestClasses()
{
ICollection<Type> classes = ReflectionUtility.GetTypesWithAttribute(_assembly, ProviderAttributes.TestClass);
List<ITestClass> tests = new List<ITestClass>(classes.Count);
foreach (Type type in classes)
{
tests.Add(new TestClass(this, type));
}
return tests;
}
It looks like it will always use the built-in TestClass.
Am I missing something? I don't how to get the test framework to use my custom TestClass
Any pointers appreciated.
Thanks
It looks like the only way to set this up right now is to write your own UnitTestProvider for the test runner. The good thing is the code is available, so it's not too hard to do this.
You can either grab the code from the default Vstt from codeplex here and make any changes you you like to it. I'm still experimenting with this right now, so lets see how this goes. I got this idea by looking at this github project.
The key was to plug in your own provider, which was done in Raven.Tests.Silverlight.UnitTestProvider.Example/App.xaml.cs.
Hope this helps someone
I'm looking for tidy suggestions on how people organise their controller tests.
For example, take the "add" functionality of my "Address" controller,
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Add()
{
var editAddress = new DTOEditAddress();
editAddress.Address = new Address();
editAddress.Countries = countryService.GetCountries();
return View("Add", editAddress);
}
[RequireRole(Role = Role.Write)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(FormCollection form)
{
// save code here
}
I might have a fixture called "when_adding_an_address", however there are two actions i need to test under this title...
I don't want to call both actions in my Act() method in my fixture, so I divide the fixture in half, but then how do I name it?
"When_adding_an_address_GET" and "When_adding_an_address_POST"?
things just seems to be getting messy, quickly.
Also, how do you deal with stateless/setupless assertions for controllers, and how do you arrange these wrt the above? for example:
[Test]
public void the_requesting_user_must_have_write_permissions_to_POST()
{
Assert.IsTrue(this.SubjectUnderTest.ActionIsProtectedByRole(c => c.Add(null), Role.Write));
}
This is custom code i know, but you should get the idea, it simply checks that a filter attribute is present on the method. The point is it doesnt require any Arrange() or Act().
Any tips welcome!
Thanks
In my opinion you should forget about naming your tests after the methods you're testing. In fact testing a single method is a strange concept. You should be testing a single thing a client will do with your code. So for example if you can hit add with a POST and a GET you should write two tests like you suggested. If you want to see what happens in a certain exceptional case you should write another test.
I usually pick names that tell a maintainer what he needs to know in Java:
#Test public void shouldRedirectToGetWhenPostingToAdd(){
//...
}
You can do this in any language and pick any *DD naming convention if you like, but the point is that the test name should convey the expectations and the scenario. You will get very small test this way and I consider this a good thing.
Well, 13 months later and no answers. Awesome.
Heres what i do now:
/tests/controllers/address/add/get.cs
/tests/controllers/address/add/valid.cs
/tests/controllers/address/add/invalid.cs