 Thanks for coming along everyone. I didn't expect this many people to be honest about such a dry topic Yeah, it is run I'll just give out a chance to sit down. Come on in. All right, we'll get started guys Let me not take this thing off So this session is obviously about tests if you're here for something else you're in the wrong room We have a show of hands who's written tests before for Drupal specifically It's got a good coverage and what about in like other testing framers like PHP, you know, all right So just a bit about me first I've been doing this about four years now. I worked with previous next in Sydney So we work with large enterprises and government agencies It's been a fair few of our colleagues presented already. I've got about 35 odd contrary modules that I Maintain in various degrees some of them. I don't do much with any more But I may have been involved in their exception But around about Shortly after Drupal 7.8 came out. I put my hand up to maintain forum Which I might be relieved of shortly because it's a shopping block to go in Drupal 8 I've recently joined the Drupal security team and I've got about 60 core commits against my name And so if you're looking to talk about anything, I'm normally in Drupal IRC in the Drupal au channels I'll Drupal contribute. I'm pretty approachable And so yeah, if you're any questions after this or next week or whenever let me know Couple of things I'm pretty passionate about at the moment I'm working to decouple the comment module from the node module So in Drupal 8 you hopefully if this gets in you'll be able to comment on any entity You can even have two comment fields on the one node and have a foreign against column and people could vote in each column And we're also working on moving blocks to content entities So you get all of the goodness of a block like you've got with been in Drupal 7 but in core in 8 So I'm plugging both of those because they need reviews the comment ones need a review since November and the block one Reviews are starting to trickle in and so if you enjoy the session Take a look at those patches. I can Set you up after their Session with some issue queues if you like But we just start talking about the goals today. I want to talk about the process taking you from a click monkey to a code monkey People kind of for me with what I'm talking about here And the behaviors have a show of hands who would think they're a click monkey who think they're a code monkey There's no no name and shaming. We're not gonna disgrace but basically if you're sort of Continuously find yourself clicking things and we want to translate Transitioning to the phase where you can run tests let them run and you know rest easier that things work the way you hope they would We're gonna talk about the why the when the what and the how we're gonna talk a bit about the Drupal dot org testing infrastructure We'll talk about some core tests contrary tests What to do when things go wrong because they will and how debug them and if we got time will briefly touch on continuous integration It's been covered already obviously There's been a few sessions on that So the click monkey way Just to define this clearly so people know you're pretty much doing everything with the mouse You repetitive you know you make a change and you check that it works in your local site And then you deploy it you check that it works in your staging site And then you deploy it and you make sure it works in your production site and you know, it's basically repeating the same thing and There's there's a lot of effort involved and you know with developers. We're lazy We we might try and avoid that effort so it might work on local so we push it up And might work on stating so push it up and go well We're not gonna click it again because I know it takes me 45 minutes to click through that and so we you know we don't do it and Yeah, it's not as easy to stop to track when something read wrong if you have test coverage And you know you push to your repository and you've got continuous integration You've got something going you see that something fails You know you broke the build you know what broke it You know what feature broke it or what bug fix and you can easily pinpoint it makes it easier to find when the regression occurred Which makes it easy to find what the regression was, but I mean creation to the converted here. This is nothing Drupal centric This is you know pretty much in general for software development And then the code monkey way, which is where we're gonna take on this fantastic journey, too You're gonna test everything with automated tests. You said and forget basically You know you'd be foolish to think just because you got test coverage. Nothing is going to go wrong you know, there's a degrees of test coverage and This is about making you know, you've still got to make sure that you've got test coverage for the things that matter and you still got to Make sure that they're actually being run and you know, they're pointless if they're not But as I said, this dovetails nicely with continuous integration and it gives you an opportunity to pinpoint when things start working Just for some light-heartedness. This is a great web comic if you're not into geek and poke But yeah, they have great You know explanations of technical terms for non-technical people the Ajax one is particularly good So I recommend checking that out But what we want to talk about is test driven development. So basically for those that aren't familiar It's you find a bug you write a test that verifies the bug and then you write the fix and then you run the test and verify The bug is gone You know sort of that process It adds extra quality work and it ensures that you know as time goes by that test coverage That we talked about gets more and more enhance So a hypothetical example well talking today We're going to go through a hypothetical example of an online classified site. So in an online classified site your typical behavior is People can come along anonymous users. They can create content. Let's just say it's a For sale items or job listings or whatever and they can't publish that content till they pay. So I've got e-commerce integration and So we're going to look at the module commerce note check out It's provides that functionality for the commerce module and we're going to be focusing on end-to-end integration So we're not just testing the functionality of that module But we're testing the functionality of that module in the context of our particular use case or our particular requirements So if you got any questions throughout feel free to put your hand up and yell out I just went to an excellent session earlier that was conducted along that matter and yeah, I found it much more useful So just on that getting off on the right foot now the test-based method isn't going to work if if you're Making changes on your production environment directly, you know, you've got to be using a code-based methodology So we're talking about things like features or configuration management and there's some good sessions on it at Today at this event You've just missed the configuration Management one I just went to that in the previous session and it highly recommended check it out online Yeah, it was basically the configuration module is the backport of the configuration management type functionality from Drupal 8 to Drupal 7 and It has a lot of similarities features but in a large environment has a lot of advantages over features and it basically is the Features is being used for what it wasn't intend for at the moment It was intended for grouping like functionality, but instead we're using it. Sorry, you know common sets of functionality or a feature And we find that most people are using it for actually deploying functionality. So check those both out online But yeah with a code-based methodology You can reproduce The same environment in your test classes as you would have in your live site. So we're talking about You know you start a project and the first thing you do is you create an install profile And you don't turn on a module by going and tick it and turning on you put it in the info install profile as a dependency You don't configure things with By clicking them in the UI you go through and you make sure that the changes are made with say variable set in an update hook Or using the configuration module you know by There's a lot of different approaches to doing it, but the main goal is that you're using a Code-based methodology to manage the configuration and structure of your site and so yeah, we've got features you've got the configuration module you can use API functions and submit handlers in update hooks and that's where the configuration update functions is staying sane session We'll talk about that more and look even in the worst case. No, you might have to do database queries to make changes, but you know Making sure everything goes through a code-based methodology Is kind of the first step to getting reproducible test environment? So I can only show hands how many people already know this and I'm just preaching to the converted like who here's not using a code base Or who he is using a code-based methodology I think you need a solid version control and again I'm probably patient to convert it here as well But something like git flow where you know your branch off you work on a feature or a bug in a separate branch in isolation and merge it back in when it's ready and You know the tests should be as they are in core a gate before that feature is considered ready to merge It doesn't come with tests or what should it be testing? so It also you know saying earlier about you can pinpoint when things went wrong. I mean if you're not using version control You really can't So yeah, we're what we're basically looking at our When you have to make your first decision you've built this classified site. You've got your Commerce no checkout installed and I've got a like an example site here, which is the Product of an install profile and so we'll be working on this install profile called sell your stuff Which basically provides all the functionality that we talked about here. This is up on github and there's slides at the end But when you install this profile you get a content type you get a product standard listing which is your pay-to-publish content product and it's already associated with particular content type and So this is what you end up with when you install the install profile And so we can use this as the basis for our tests to ensure that we're in the same reproducible environment every time So back to the presentation This is where you got to make your first decision. You've built the site. You're ready to go I mean the first thing you want to check is that the checkout process works, you know your client It's paid you to build this site This is their revenue stream. They want to be sure that you update Commerce or another contributor module on your site that your checkout process isn't going to break and they're going to call you in The middle of the night so what's going on? So this is where you make decision manual tests or automated tests And I'm sure most people out of the room here would know what that number is for Yeah If you're testing your checkout process by typing that number in you know what I'm talking about and this is when you know Should maybe be looking at and moving to this automated testing There's other things people might be using macro plug-ins for Firefox or crime where they record your clicks and you can run them Form auto completes, you know Look before I used to work this way I know for a fact I could go to any commerce checkout form on any site and start typing it Recognize the fields and it'll provide me with 4111 So yeah, what this is where you got to make the decision and weigh up the costs involved with both So when you want to test well budget is a factor, but you also have to weigh in maintenance cost There's a cost associated with doing that manual testing and there's also a cost associated With when things go wrong and if you're after you know responsible for the maintenance of the site And you constantly got regressions or bonds that you should have test coverage for there's a cost involved in that as well And that cost I would say would offset the time it takes to write the tests and do it properly from the start And there's other things that you've got to work way up here. You've got click fatigue I mean you don't want to RSI injury from going through the checkout process and You know you need to decide Which things that you want to test and you want to based on their importance And so I talked about here the checkout process. That's obviously the critical functionality of this site You don't need to test that you know people can log in you don't need to test all those things that's kind of the drill You're really interested in the end-to-end integration. That's specific to your business case here and What it does help you do is is keep a lid or limit the risk to regressions and it Obviously provides you with an extra string in your bow for quality when you're selling to the client that We practice test development. You know we back up our Work with tests and we can you know it gives you that extra bit of prestige into what you do And so what do you want to test? Well the general rule you've got a test critical functionality We talked about that but new tests for new features your branch off your git flow model and you create You know you've got a user story, which is a certain set of functionality and you go test to go with that You've got peer review. I'm certainly a previous next. That's one of the gates before during peer review is is there a test? Should there be a test? Why isn't there a test showing me a test? you know and Yeah, when a when a bug is identified as I said before write the test verify it fix the bug run the test and you Know hopefully you should never have to think about that bug again If you've got continuous integration, you know straight away if someone's broken it So how do you get started? Well, you probably do have to do one manual test run because you need to work through logically in your mind The steps that you're testing for your end-to-end So in our example you would work through the checkout process and you'd have a pen beside you Or you'd have your code editor open and as you click you do the equivalent in Your test class as you submit a form you do the equivalent and so we'll go through that in detail shortly But when you see them side by side it kind of makes it makes sense It's it's not that it's not that hard to grasp But we'll get the basics ready first. So first thing you've got to do is Extend the base classes with your new class and so this is rocket science people for me with it In Drupal 7 you've got to register your class with the with the registry So is everyone familiar how you do that in Drupal 7 basically you've got to declare the file in your info file So with the files array and your file has to end in dot test and clear your case and you're good to go So Drupal 7 we would ordinarily extend the Drupal web test case. You create your test file suffix it with the dot test extension You need to declare to Drupal what your test does and so your class needs to implement the get info method and the get Info method is like an info hook and most people who write Drupal modules or have written Drupal modules would be familiar with an info hook It basically tells Drupal the name of your module what group it belongs to and what it does And then you set up your tests with a setup method. So every Method in your test class that is prefixed with tests. So test widgets test foo bar That's considered an individual test by Drupal and when the tests run it runs each of those instances Before that it runs setup method and so in your test class You can use the setup method to get the environment the way you want it for that particular test Now most cases you that would include logging a user in because you know, most of the functionality providing Drupal is for an authenticated user and so One thing you always going to do in is Install the required modules right your your your module might define In its info file its dependencies, but in a case of say an install profile You might need other things that are specific to that test So yeah, you've got to you got to take care of the housekeeping and I've seen places where the dependency chain doesn't work flawlessly So just be mindful of that if something doesn't work the way you thought it would just you know make sure you've got all the All the modules that you need and I'll talk a little bit about that how to debug what's going wrong surely so Drupal 8 and This is especially important for people who want to get involved in core or who are coming to the core spring You need to extend Drupal simple test web test base now This is PSR zero namespacing and if you've been to any of the sessions on say symphony or web services You wouldn't seen that before I'm not going to go into great detail, but there's some Differences between Drupal 7 and Drupal 8 in Drupal 7 you have to set up You have to call parent set up in your setup method and pass the array of modules You need enabled and I'll show example that shortly, but in Drupal 8 It's a static property on your class Which is an array and it's a ray of the module machine names and that's certainly much simpler and in Drupal 8 We also have a testing profile, which is a stripped down install profile if you're writing tests for core You should be considering using the testing profile Other than say standard or minimal, you know if you're testing the functionality of a particular module And you don't need node installed You don't need comment You don't need the two content types and the search block placed in the left-hand sidebar and the powered by Drupal in the You know use the testing profile because every one of those test methods in your class is a complete Drupal install what it will do is it will spin up a whole new install of your site in a prefixed Database class so it creates a new connection and it uses prefixed tables Which people were probably familiar with it was the big whiz bang thing in earlier versions of Drupal So if you can minimize what that has to do on install every time Your tests are going to be a hell of a lot faster And so obviously for core where tests are taking close to an hour at the moment Yeah, the test suite for core is taking close to an hour to run We have an enormous amount of tests. Yeah, if possible is a testing profile And if you're doing in a simple tests of your module functionality when you get to Drupal 8 It's available. They're 40 years And I've got some code examples of Drupal 8 which I'd like to show you now to see the main differences and so First of all you your namespace if your PSR zero use the web test space and this is a tracker test from core and we're enabling the comment module the tracker module and the history module and They're the tests that are needed for this Test that modules that are required for this test We have detected very quickly user another user. So this is the get info. This is the same as it is in Drupal 7 you return an array the name of the test description and what group it belongs to and the group is a powerful feature if you're writing tests for your for your project for your Requirements if you use the particular group that's helpful for you to run them in a targeted environment with say drush and continuous integration You don't need to run the full suite of core tests against your project Just need to run the ones that are particular to your project. So when we get to shortly how you actually run the tests I'll show you where that is useful because yeah It means you can pinpoint exactly the tests that you want in a in a nice chunk And so we'll just have a quick look through the setup method on this Drupal 8 one sets up the parent because it's inheriting and Sets up a few things for this test. So this test requires a content type type page It requires a user with access comments create page content post comments and some other stuff off the side And it creates it it needs two of those users and it needs some variable set So that set up method runs for every test that are in this class and then so he's one test Here's another test and each one of these runs in a separate install. So yeah, you can see where Getting this Commonality between the three could easily go into the setup method. So you're not repeating yourself And I've just put a lot in there Okay, so back to our checkout pricing We would follow through to logical manner So the first thing we'll do is we would log as an anonymous user We would navigate to the ad content page. We would fill in the form We would hit submit we would verify that we're redirected to the checkout page We would fill out our address details. We would hit continue to the next step. We would put in our Credit card details and we would complete payment And then we might log in as the admin user and make sure that the node that they created was published We'd go to the order section and make sure that the the order came through and the price was right Is this you know kind of follows through logically? Yeah, it's pretty yep so what you do in the browser basically and What does that look like in code? So this is the test for the the sell you stuff module install profile in our setup We're also requiring commerce or UI I'll get to why in a minute But we don't want the order UI on our production site for this particular install profile So we can actually enable additional modules that aren't in the install profile better. We need it for the purpose of tests We're going to create a user for the different operations that we need. So Not a good choice of background Here we go So admin permissions we want to be able to access the content overview We want to administer nodes we want to view some commerce orders and we want to administer the orders And so we create an admin user against the test by going this trip will create user and passing in the permissions And so we want to what we're basically doing is we've only got one test in this class, but You know as I work through them sequentially before Is this everyone can read this? It's not too small Sorry guys So we're not logged in first thing we do is we go to no date page So we use Drupal get and it performs a get request with simple tests internal Browser and goes to no doubt page and if you run these in the UI you actually get the HTML For no doubt page and so you can see there's any warnings what went wrong. It wasn't what you expected to see Get to that shortly And so the first thing we want to make sure in our test is that Anonymous users can actually get to that page because ordinarily they wouldn't be able to write That's you know create content is normally protected Permission and and so if our install profile is doing what it should the anonymous users should get a 200 there They should get a valid response not a 403 not a 404 So we assert that the response was a 200 and Then we want to create a listing so we're going to sell a lovely three-year-old miniature pony here named rainbow sparkles And so we've got a title and we've got a body now those people who've worked with the form API We'll probably recognize the format of this and those people have worked with the entities in triple seven probably know and love language none Or or hate and if you went to most of session on web services on Yesterday first session and he talked about entity next generation If you hate language none and you hate this zero value You're going to love entity next generation because you go node title value and you get the value You go node body value and you get the value and If it's an you got if your language content is Italian you go node body value and you get the Italian version It's for a developer experience. It's Bullismel, you know So basically Say you don't know the form API or you don't know the structure of the node and you want to find out What am I what does this edit thing supposed to look like? What does this voodoo? Um We're back. Um, you can actually look at the markup so I'll set up a Anonymous user, you know, I'm in a private browsing window here. So I know I'm a novice New dad article What the ad content basic page here we go So I'm gonna I'm gonna create my miniature pony, right? I really should watch my Yeah It's what happens with So it happens with children and we're demonstrating it like a screencast to a client once and Yes, I'm a came up. It was really not right But yeah, you've got young children and they're sitting near you say what shall I use here? And they just spit out the randomest things. Yeah, okay So how do we know what what was supposed to be submitting there if you inspect the elements that you're submitting and you Look at the name attribute in the DOM That's what you're submitting. That's what your post variables need to be keyed by so body UND which you would know is language done zero value Incidentally in Drupal 8 it's now language not specified Just because we know you like Change yeah, yeah, no, it's actually to standardize with Everything like this is actually standard for nominating that So we want to post that form so The arguments to the post are where you want to post it to what you want to post and what button you want to click because As you saw on the node form back there. There's a couple of buttons save and preview So you need to actually pass those in and you'll notice I've wrapped them in tea and that's just Good practice because that button could be translated if you were working in a multilingual environment But the null the reason we're passing null is because we already navigated to know dad page earlier Back up here at line 54. So we don't need to navigate there again and Drupal's internal browser in simple test smart enough to know that and So we want to check that things work. So if I Go back to that example See these notices here basic page miniature has been created and statin lists into your cart So if you're doing this manually that would be the first thing you check that it kind of worked the way you're expecting So we just go looking for those You can make a bigger point there is We're checking for basic page miniature pony has been created and the starting listing was added to your car now Because I'm actually hard coding the miniature pony up here. It's safe for me to do that But if I was using you know a variable something I'll probably use something like format string to make sure that you had the string Injected the same way that Drupal was doing it replicating what was actually happening in the module so that you get the same It's going through the same pipeline basically and you get the reproducible result, but what I'm doing is here This is going to be like can you see it? Where's Wally there is a Cert text is checking for text on the page now There's a lot of other methods which we'll get to shortly But a certain text is if you're checking for tests and a cert raw is another one You can use that will check for markup because a cert text It's been passed through plain text Check plane which everybody knows Hands up. Yeah, if you know what check plane is. Yeah, basically it sanitizes. It's just a wrapper around HTML special Charles so we're checking for both those. That's so good. Have a go for time And then we just verified that our URL is of the format check out With it with a number after I'm not going to go into detail what preg match does because we'd be here all day But basically a format goes to check out to and so that's what we're we're interested in we're making sure that we've gone to the Right URL so we can get the URL that the browsers on from this get URL And if we've got two matches like you would with preg match, we've been redirected if we wanted to get tricky We could assert that there was a 301 redirect there So Probably rightly point out this might be a little bit fragile, but I'm assuming that the first node that's created is node one But because this is a fresh install I mean this is from this point on it's installed a friend brand new site The first node is going to be node one and so it's it's relatively safe to do that There's a lot of things in core that would Say you submit a note it would match on the on the path same as we got the order ID in this preg stuff But yeah, in this case, it's safe And we want to make sure that the node is not published because the intent of this site is Someone goes in they can create a node But no one else can see it until they actually fork the cash out So that's what we get up to here. And now here we start to submit the check out form now We inspect the check out form like we did before and we look at our elements And we get the names of them Hang out some big ones there And we make sure we've got the same thing in our Values, but basically that line 80 down to line 92 is one step of the check out process And you want to know how fast simple tests can do that compared to you fill it out by hand Yeah, a lot faster. So, yeah Everyone familiar with dribble commerce. It's a two-step process by default and both the buttons say continue to next step Even though the last one actually submits the form So the first step for the address second step payment third step submit it I've commented the code here because this is available on github and yeah, this is actually complete payment Right, just it seems weird. And so we check the check the we assert that the thing is complete now This is gonna really I'm not gonna actually go through that because we're short on time But believe me when you commit that when you submit that that's what you get Check out complete. So we go looking for that make sure it's there and we use format string here to pass in the Commerce order number which we got earlier up here and the reason we do that is as I said before That's what commerce is doing internally if we just check for your order number is number you might get Inconsistent results. So we're possible use format string and that's identical in arguments to the t function Which is most people would be familiar So now we want to just do the admin side of things we log in his admin We go to admin content node and we make sure that miniature ponies on that site and we make sure there's published Now, this is probably You know if I was being honest a little bit fragile to look for the word published on that page But because the sites just been freshly installed and it's only got one node If it wasn't published, you know, you wouldn't see it So, yeah, we load the node back up We use the true argument here at the end to make sure that we don't get that one from the static cache And we should Assert that the node is published now and then just for some extra You know to make sure the price was right we go to the commerce order page We make sure that we get a 200 there and it's worth pointing out that if you use Drupal get to navigate to admin commerce orders for Slash view if that order didn't exist You won't get a fail Because that might be what you're trying to test you might be trying to test that that's an invalid URL So you have to actually assert that it's a 200 to make sure that that page actually exists You would get a fail because the text you're looking for later on doesn't there isn't there, but yeah If the the URL that you navigate to with the internal browser doesn't exist or is accessed denied It won't the Drupal get won't throw a fail because that might be what you were expecting to happen And we just check for whoops find a replace fail there And coding standards fail no Yeah, sorry, I did a run through this presentation and Someone rightly pointed out that I shouldn't be using UND. I should be I'll push that up after so yeah That's kind of working through it sequentially So we looked at some of these before you can check for raw output with the cert raw and you can check for plain text with with the cert text I Said you should check for specific markup You're expecting but you need to be aware that you're not picking something that's going to change from time to time So if you're looking for a piece of markup that goes through a theme template and you know Joe blogs the theme art goes and changes the markup because he wants to use some different, you know something different You know your tests might fail and it's not a failure in its true sense of the something's broken But it's a it's a failure that you know, so he's going to have to go in and modify the test So if you can pick something that's specific, but not too specific Yeah There's a couple of the utility methods this file in this pass and they're kind of things that you would use in your logic flow so say for example, you got to try catch block and You're expecting it to throw an error and you end up in the catch block you would this pass But if you didn't throw an error Well, and it was supposed to you would this fail. So yeah, there's nothing you're actually asserting for it's just in your Logic in your code you're finding places where you're expecting particular behavior And there's a great huge number of Assert helpers, so you've got things like a cert field by ID And so that will make sure that there's a field on the page by passing at the ID. It's a cert field by name There's a cert option is checked. There's you know, there's a myriad of Assert helpers that I'm not going to go into here But if you're interested and you you're going to do this sort of thing Look at the base class and read the code and that's always with anything the best way to learn it and There's one other powerful feature which is XPath Hands up who's familiar with XPath? Yeah, okay. It's basically an XML passing language. It's kind of like jQuery, but kind of not Yeah, if you Google XPath syntax, I think you need to ask the IRC bot you'll get a link to it Yeah, it's it helps you with the kind of thing we're talking about for with a specific markup that you're searching for but it's not It's not that Intuitive how it works if you're not familiar with it and I Find it painful So there's a lot of other helper functions you saw before in the tracker test. There was Drupal create content type So yes, you could create an editor ray. You could log a user in as someone we've administered content types You could go to add content time you could submit the form But what do that when you can use Drupal create no type or Drupal create no Drupal create user there's a lot of these there's cron run there's click link and Drupal get node by total title is probably the method I should have used in that test where I instead of going node load one could have just noted load Loaded the node back that Was titled miniature pony? Okay, so we said before testing all of core is slow There must be a loose cable over here. How do you actually run the tests? Look, I would I would advocate the test testing the automatic test is far quicker than doing it Manually, but it kind of depends on what you're doing in your test and if you have got stuff in your test where your for example Got features involved in this features rebuilds that can add a fair bit of time to the test because Soon as the install occurs with each of your test methods a chron run fires and a chron run fires of features rebuild And the features rebuild can take quite a while So yeah be be mindful of that and if you've got things in chron that go off and fetch web services Or you know update module you need to be careful because you know, that's run at the very start of every test And if you can trim those down, yeah do so so to test it You need to enable the simple test module and this comes in core in Drupal 7 in Drupal 6. It was a contrived module I'm just making sure it's turned on It's there. You might not have seen it before and Then you head up to your configuration tab and you've got a testing tab and here's all the core tests and there's quite a lot of them and Here's our tests and they show up in the groups that we showed before and so you can click through the device to do this Or you can also use drush and the command is drush test dash run and you have to pass the minus L argument Or you need to have that in your drush Rc. So it knows what the actual URL of the site is because it is performing with a simple test browser navigating to and from sites it needs to know but if you've got your Tests grouped you can just say drush test dash run minus L example dot com Say stuff and it'll run all the tests in that suite and so There's a minus minus XML flag so you can integrate with You know continues integration with the test results, but yeah, basically click it run it and let it go and like We want to have a get how long you think it would take you to do it manually probably a minute two minutes three minutes It comes back pretty quick for a small set of functionality and we're testing from end to end here If you're doing it manually, you would probably I don't need to do that step. I'm skipping that step. So yeah It's gonna prove me wrong Yeah, if you're doing 50 tests especially yeah, but if you're doing 50 tests, it will take longer And as I said if you've got features it will take even longer again And so I'll let that run and I'll come back These are some special cases that I think are worth note most sites these days would have image integration and Getting those isn't obviously straightforward. You know, you would click on the browse Button on a form upload and you would attach an image and you would hit submit and you know The image gets sent from your computer, but you can use that chunk of code there the simple test space class has a swag of test images and other files you can access and If anyone's used to Val generate it's just that same little square image with the circles in it and Yeah, you'd what you do is you get the image the code at the top and then you use Drupal real path and you put that in your post variables and you send that through and The media module uses an FID so you can you have to actually save that image first to get an FID So yeah, you pass that to file save the actual image and you get an FID back I just thought those were worth a special mention and things go wrong and Look the debug function is your friend things like if you don't have access to a debug as in You know look you're running on a virtual machine, or you haven't got it set up the debug will let you output things through your code You can't use things like DSM or DPR or KPR or all the different flavors of crew mode because Yeah, it's just it's it's not available in that environment. So debug is the go And look people will probably gasp at the third line But don't be afraid to go in if you're getting some random error about some module That's or some permission that's supposed to be there And it's not fair go into the line and code in call that's causing that warning and put some code in there You've got your site under version control. You're just going to check that back out anyway You're not really hacking it. You're just learning about it, you know and watchdog isn't available because It's every site spins up its own database and so watchdog is writing to this new database every time we've got a Drush plug-in called watchdog simple test you can get and put it in your drush dot drush folder and it gives you all of your drush watchdog commands just add an extra s to the end so drush watchdog show drush Like it's drush ws drush WSS will look up the prefix that simple test is using and get you the Values from that actual install now You need to make sure that DB log is a required module in your tests so that stuff's actually going into the DB log table But it is handy particularly with features and if you're wondering why something's taking so long drush WSS minus minus tail which works like tail does in the command line and minus minus full So you get the full app it and you'll just see what it's actually doing So if you're sitting scratching your head going what it's not doing anything, you know go in and use that and you can see Rebuilding feature rebuilding feature Okay unit tests we haven't really talked about that much but a unit test is like when x comes in y goes out and so there's unit testing core for things like Image style effects, you know like if I passing an image of size 150 by 200 and I've got an effect. It's got a resize. It's a 90 by 90 I should get an image out the other side and These are things where you don't need the internal browser of simple tests I have the of the web test that you can navigate to pages and things. It's just testing the functionality But there's no database. There's no files. You can't enable modules. You can't use a watchdog module implements any of the hooks There's no database, you know but These are way faster and so if there is functionality that is atomic that is utility use it wherever possible, but you need to take care of loading files and modules that you need and Yeah, you can fake enable modules and Thanks to whoever I can't recall where I got this from but basically it's just hijacking the module this module to function to fake enable a module and There's a limited set of assert methods as well There's no assert field by ID because there's nothing to assert it against Drupal 8 has a new class called unit tests remastered if you're interested check out that that node, but it adds a new Class called Drupal unit test base and it is like a mock implementation of a lot of the Drupal functionality So there's mock hooks You know mock module installation Yeah, a lot of them are sort of pure empty implementations So it's obviously much faster than the web test space And so if you need some of the Drupal isms that you would that your module is going to operate in but you don't need To navigate anywhere with Drupal get and Drupal post you should use the unit test remaster. So I want to talk a little bit about What have we got 45 minutes here? I might skip over this if you maintain a contributor module you can enable this For your contributor module so that you have automated testing of your branches If you want to talk to me about that come and see me after but you can also do the same thing in your issue queues So that when people put up a patch they can see straight away if it doesn't pass. I have another 15 to it score Let's see if I can go back Yeah, so basically if you head to your module and you go to the version control tab Sorry the automated testing tab you can see the results now. This is an empty module. There's no tests Zero passes, right, but it's there for you to use You can also head to the issues tab under the edit tab and hit enable automated testing And so anytime anyone uploads a patch to your module and they choose needs review The test spot will go off and test that and you know it saves People's upload something won't apply anymore. It's got a fatal in it You know if it doesn't pass the tests and you know you can probably delay looking at it a little bit longer Okay, and this is the plug for tomorrow getting started with core tests Like who's uploaded a patch before to triple org that fixed a bug, but you know, it's been told Well, it needs tests before it goes in. Yeah So I've been in that situation before my first core patch was just before 7.0 came out to fix the problem with the theme pre-processing like the theme suggestions weren't working and I Went right through core and I found this issue. Oh great I've solved something I put it up and got knocked back needs tests and so that was my first exposure to writing tests for core and To be honest as with anything in Drupal the more you read The more you learn you see example code and you dig into it and it you know, that's where the light bulbs go on so If you want to get started check out the needs tests tag and there is a tag Specifically for issues that are fixed but they need a test and if you write a test for it You know upload two versions of the patch one with the test call it Dot fail dot patch and one with the with the test and a fix called dot pass dot patch and upload them in that issue Put the put put them in that order put the file one in first put the past one in next The test spot will test both of them the first one to come back red because your new test verifies the bug And the second one will come back green because the test plus the fix it sorted find the needs work issue there's a lot of people who put a lot of time into patches and They get towards the end and you know There's just one little test that they can't get to pass and I'm sure those people would love Someone who wants to get started to come in and just find what it's causing it You know and spend the time debugging it and come on and you come along and you'll be the hero You know people will love you so come along to the Sprint Down Saturday if you like this kind of stuff And there's Yeah, a specific task that the core mentoring process has called needs tests and the people involved Jess and Andrea and Kim they actually go through and you know create these tasks for people who want to learn core who want to write tests and You know they can get you started straight away the next level is Continuous integration and sorry me. I didn't clear your name, but Yeah, what what we do a previous next we've started thanks to me a nick down the front here and meet up the back there is when we push to the master branch Jenkins Fires up a test run against a separate install We have specifically for testing runs the test suite and comes back green if it passes or good comes back red Everyone gets an email you broke it You know and then Kim rings us up and says fix it And yeah, look There's various ways to do that, but Drush test run is great And so if look at I don't pretend to know anything about this if you do want to know more Boris had a session yesterday about it and Kim Nick and me Provides more information So yeah, that's it for that. I'll just go back to the test results Can anyone see it? I think the problem is the The screen resolution doesn't fit out of the projector yet. So you can actually go outside the visible area. There it is It's green and if you work in an office with people who run these tests you will Mix the arthing down the front You know, it's kind of like that And so this is what you get right? These are all of our tests We enable the modules we created the permissions we created a user and what did that look like well You can actually click on it and it will show you it's got and this isn't just a static page This is the actual marker. And so if you're expecting a particular thing Overlay you bastard I'm gonna have to run them again. Yeah Yes, and that's So there is actually a tag for core issues called needs manual tests and that's the JavaScript ones and Yeah, I'll come back when that's finished. I'm really annoyed that because So there's a more information that whole install profile and all those tests you can get on github I've still got to push up those fixes that I just made then the stuff that's up there now works Okay, I'll fix that out. Thanks man. I'm gonna run a boff after this session If people want to talk about this in more detail, but also with the focus on core And if you need help getting the stuff set up for tomorrow, and you want to sort of hit the ground running happy to help and Jess up the back did a similar presentation at the Midwest developer summit about this hers is purely focused on core and it's Yeah, and it's actually a lot more detailed than mine, and I didn't see it till the end So I didn't pleasure as any of it. I promise Hey, that was quick 42 seconds. That's the answer to that question. How long would it take you to do manually? I'd say a lot more If I was in RC, I could ask our bot. We have a bot and he will answer that question So for example, you can click on verbose message and without overlay it opens in a new tab and You can actually see at the top a get request to that and that's where we ended up And this is what the markup look like and as I said, this is the full markup You can previously next through it and you can see what was submitted. So there's the the password for user better figure her and Yeah, there's a check out complete you can see the credit card number that we talked about before and Yeah, that's kind of it Any questions Yeah, this is yeah I'll just repeat the question the question was what happens if you need to run tests in the context of a Particular site with live content and users secret. Yeah, it is it is difficult question. You'll you noticed in the In a test that we did before we created the node, you know, we actually went to the Provisioning I guess the environment with the content that we need there is other approaches and I haven't talked on them here there's Is it cool is that what it's called that? Yeah, there's a there's a work that I think it was more She's been working called upal, which is I think in the context of that There is upgrade tests in core which actually use a dump Script to dump a database and the first step is to install that database, but I'm not sure how Why is it would be to use that? Because it's it's you got a you know a tire pardon me you've got to have a Tars gzip of The database dump that can be loaded by the test before the tests even start, you know, and so if possible You should try to I mean When you test something locally in your local environment, you would only create the node that you would test with You know, everyone's got a Laura Mipson generator, you know, and you actually generate dummy users and dummy content in general Yeah, yeah, so it's kind of that sort of stuff you have to do in your tests before in your setup method or in similar, but Yeah, you're saying you want to test that sort of stuff. Yeah, well, you could you could do that because Yeah, you just have to create a dummy user first and then you know submit the form values and make sure you ended up We were you expected and actually if you saw in that that's actually what it does with that this Drupal logging Sorry, I didn't talk about this Drupal login Was in the code but this Drupal login is actually just a utility method that actually does what you just said It goes and logs the person in and if you have a look in these Where are logged in as the admin user it actually goes And does that so if that was in the context of your install profile that enabled some particular module that did a redirect after logging You wouldn't end up at say stuff user to you know what I mean you would actually end up Where you're supposed to end up and you could assert that you got 301 and that the message that you're expecting with it So, but yeah, it is it is like a question particularly in the context of Nodes and stuff. Sorry, just behind you So just to repeat the answer for the tape just was saying then that If you're looking for that sort of thing you probably want to look at something else. This is more kind of it You know, it's it's not unit testing but for better sense of words like Testing the workflow in a fixed environment. Is that kind of paraphrasing? Yep. Sorry Look, I've been plugging it all through lunch. I was very happy with that session on If there's an error it'll have you got much time left No Two minutes. Well, if you want to talk to me after but if we mock that test so failed So we check for $29 or something You still get one of these Even if it fails, you still get this and you know, if there's an exception or a warning It'll fail and you'll see the Drupal set message of you know undefined indexed foo in line 74 of you know Some file like if an error occurs in the test for a PHP warning exception a fatal error It'll fail and the green green green green green That we had before will actually be green red green red green, you know, and you have a big red line and it'll say you know Roar your order number one found and will be in red and you'll be able to check on that the most message and so on so it actually I Think it's better than a screenshot But yeah, I mean one thing this doesn't test is the visual aspects and that's often a large part of What we do, you know making sure that the buttons line up and that you know the JavaScript does what it does So yeah, this isn't the answer for everything, but yeah Hey, I We've I'll talk to you later, but yeah, we there is this in that stuff we guys did we did together like the end-to-end testing of all that Which is more of a JavaScript so like you're waiting for the dom to load so the question was with selenium you have to wait You're getting fails and it's a timing issue It's based on curl correct me if I'm wrong Susan curl So it's actually doing a performing a request. That's what same for if you run it through drush You've got to have you know, you've got to pass it the URL that you're testing against and Nick down the front with the test that you know if you've got HT or running, you know, you've got to configure it There is actually a settings tab No, no if you're getting a time-at-issue It'll be a time-at PHP max execution time time-at-issue Yeah, you can you can pass HT authentication stuff there You know if you're if your server is behind HT authentication This simple test is outside using curl to make the requests and so if it can't yeah I mean this simple test is a druplism. It's you know, we kind of we still maintain a lot of this Now don't we Yeah, I mean, there's what more than 25,000 tests there so If there are better solutions and what was said then is that we we're currently maintaining this just for Drupal But to move to something else is a big undertaking. There's two and a half thousand test methods and fifty thousand asserts So yeah, that's a that's a huge task