2. UK based C# MVPWeb Developer @ 7digital.com Working on a number of Open Source Projects Co-Author of Testing ASP.net Web Applications http://www.testingaspnet.com
6. It is 2010. Automated testing is no longer controversial.
7. [TestMethod()] public void DebitTest() { string customerName = "Mr. Bryan Walton"; double balance = 11.99; BankAccount target = new BankAccount(customerName, balance); double amount = 11.22; target.Debit(amount); Assert.AreEqual((System.Convert.ToDouble(0.77)), target.Balance, 0.05); // 0.05 is tolerance for floating-point comparison //Assert.Inconclusive("A method that does not return a value cannot be verified."); }
8. [TestMethod()] public void DebitTest() { string customerName = "Mr. Bryan Walton"; double balance = 11.99; BankAccount target = new BankAccount(customerName, balance); double amount = 11.22; target.Debit(amount); Assert.AreEqual((System.Convert.ToDouble(0.77)), target.Balance, 0.05); // 0.05 is tolerance for floating-point comparison //Assert.Inconclusive("A method that does not return a value cannot be verified."); }
9. [Story] public void Should_find_customers_by_name_when_name_matches() { Story story = new Story("List customers by name"); story.AsA("customer support staff") .IWant("to search for customers in a very flexible manner") .SoThat("I can find a customer record and provide meaningful support"); CustomerRepository repo = null; Customer customer = null; story.WithScenario("Find by name") .Given("a set of valid customers", delegate { repo = CreateDummyRepo(); }) .When("I ask for an existing name", "Joe Schmoe", delegate(string name) { customer = repo.FindByName(name); }) .Then("the correct customer is found and returned", delegate {Assert.That(customer.Name, Is.EqualTo("Joe Schmoe"));}); }
26. [TestMethod()] public void DebitTest() { string customerName = "Mr. Bryan Walton"; double balance = 11.99; BankAccount target = new BankAccount(customerName, balance); double amount = 11.22; target.Debit(amount); Assert.AreEqual((System.Convert.ToDouble(0.77)), target.Balance, 0.05); // 0.05 is tolerance for floating-point comparison //Assert.Inconclusive("A method that does not return a value cannot be verified."); }
27. describe BankAccount, “Transactions” do it “can debit pence from an account” docustomer_name = "Mr. Bryan Walton"; current_balance = 1.50; account = BankAccount(customer_name, current_balance).newamount_to_debit = 1.25; account.Debit(amount); account.Balance.should == 0.25 endend
28. describe BankAccount, “Transactions” do before(:each) docurrent_balance = 1.50; @account = BankAccount.new("Mr. Bryan Walton", current_balance) end it “can debit pence from an account” doamount_to_debit = 1.25 @account.Debit amount @account.Balance.should == 0.25 endend
36. Feature: Creating a schedule In Order to book meetings The manager Needs to be able to view a teams calendar Scenario: View calendar of another view GivenBob has a public calendar When the manager views Bob’s calendar Then an empty calendar should be shown
38. When /^ the manager views ([^amp;quot;]*)’s calendar$/ do |user| end
39. Then /^ an empty calendar should be shown $/ do end
40. [Story] public void Should_find_customers_by_name_when_name_matches() { Story story = new Story("List customers by name"); story.AsA("customer support staff") .IWant("to search for customers in a very flexible manner") .SoThat("I can find a customer record and provide meaningful support"); CustomerRepository repo = null; Customer customer = null; story.WithScenario("Find by name") .Given("a set of valid customers", delegate { repo = CreateDummyRepo(); }) .When("I ask for an existing name", "Joe Schmoe", delegate(string name) { customer = repo.FindByName(name); }) .Then("the correct customer is found and returned", delegate {Assert.That(customer.Name, Is.EqualTo("Joe Schmoe"));}); }
41. Feature: List customers by name As a customer support staff I want to search for customers in a very flexible manner So that I can find a customer record and provide meaningful supportScenario: Find by name Given a set of valid customers When I ask for an existing name Then the correct customer is found and returned
42. NOT BEST PRACTICE!! Given /^a set of valid customers$/ do @repo = CreateDummyRepo()endWhen /^I ask for an existing name$/ do @customer = @repo.FindByName("Joe Schmoe")endThen /^the correct customer is found and returned$/ do @customer.Name.should == "Joe Schmoe“end
53. using Cuke4Nuke.Framework; usingNUnit.Framework; usingWatiN.Core; namespaceGoogle.StepDefinitions { publicclassSearchSteps { Browser _browser; [Before] publicvoidSetUp() { _browser = new WatiN.Core.IE(); } [After] publicvoidTearDown() { if (_browser != null) { _browser.Dispose(); } } [When(@"^(?:I'm on|I go to) the search page$")] publicvoidGoToSearchPage() { _browser.GoTo("http://www.google.com/"); } [When("^I search for amp;quot;(.*)amp;quot;$")] publicvoidSearchFor(string query) { _browser.TextField(Find.ByName("q")).TypeText(query); _browser.Button(Find.ByName("btnG")).Click(); } [Then("^I should be on the search page$")] publicvoidIsOnSearchPage() { Assert.That(_browser.Title == "Google"); } [Then("^I should see amp;quot;(.*)amp;quot; in the results$")] publicvoidResultsContain(stringexpectedResult) { Assert.That(_browser.ContainsText(expectedResult)); } } }
54. Given /^(?:I'm on|I go to) the search page$/ do visit 'http://www.google.com' end When /^I search for "([^amp;quot;]*)"$/ do|query| fill_in 'q', :with => query click_button 'Google Search' end Then /^I should be on the search page$/ do dom.search('title').should == "Google" end Then /^I should see amp;quot;(.*)amp;quot; in the results$/ do|text| response.should contain(text) end