Testing in Laravel | How to Write Tests With Laravel | Laravel 8 Testing Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ค. 2024
  • This video will show you how to use test your code in Laravel. We are going to cover the basics of unit tests, feature tests, HTTP tests, database tests and seeder tests in Laravel.
    Resources
    - Github: github.com/codewithdary/larav...
    - Patreon: www.patreon.com/user?u=30307830
    - Instagram: / codewithdary
    - Twitter: / codewithdary
    - Cloudways Hosting: www.cloudways.com/en/?id=918573
    - Items I use: kit.co/codewithdary
    Get up to 1 Month Free Hosting by using code "DARY15" and get $15 free hosting credit.
    Cloudways offers peace of mind and flexibility so you can focus on growing your business and reaching new milestones instead of dealing with server management. With Cloudways, you get an optimized stack, managed servers, backups, staging environment, integrated Git, pre-configured, Composer, 24/7 support, and the choice of five cloud providers: AWS, DigitalOcean, Linode, Google Cloud, and Vultr.
    Coding Gear
    - MacBook Pro: amzn.to/3qTMnGi
    - Dell XPS: amzn.to/3Atp7Cm
    - LG 34WN650-W: amzn.to/3G0XMbS
    - Magic Trackpad 2: amzn.to/3Arp2ir
    - Magic Mouse 2: amzn.to/346k5zC
    - NAS: amzn.to/3rFf60U
    - Sandisk SSD: amzn.to/3fNHcBB
    - Rain Design Stand: amzn.to/3GTMeZ5
    Recording Gear
    - Rode NT-USB [Start]: amzn.to/3ApApXY
    - Shure SM7B [Advanced]: amzn.to/3Arj5lv
    - Rode PSA1: amzn.to/32pMCQ5
    - Focusrite Scarlett 2i2: amzn.to/3FYZD0t
    - XLR to XLR: amzn.to/3qVm2rC
    Timestamps
    00:00 introduction
    01:57 Sponsor of this video: Cloudways
    02:34 Introduction Part 2
    03:09 Integrated Laravel tools
    05:10 Running your tests
    07:50 Our first failed test!
    09:48 Creating Unit Tests
    16:46 HTTP testing
    20:17 Database testing
    24:55 Seeder testing
    ▬▬▬
    #cloudhosting #managedhosting #cloudways #laravel #laraveltesting #phpunit

ความคิดเห็น • 109

  • @codewithdary
    @codewithdary  ปีที่แล้ว +1

    If you're looking to level up your Laravel skills? My new course "Mastering Laravel 10 Query Builder, Eloquent & Relationships" is the perfect opportunity: www.udemy.com/course/mastering-laravel-10-query-builder-eloquent-relationships/?referralCode=C784C3DB5B8925207FCB

  • @amirhosein4728
    @amirhosein4728 2 ปีที่แล้ว +6

    I am absolutely in love with your channel and I am learning laravel right now because you teach so damn great 😄

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Thank you so much Amir, appreciate it! :D

  • @muzika5001
    @muzika5001 2 ปีที่แล้ว +1

    Great! Thank you for introduction in the testing.

  • @mokolokoa3288
    @mokolokoa3288 2 ปีที่แล้ว +10

    One thing is missing. How to set up test database. (if you already have some data) So the tests will use different db and then they will wipe out the test db.

  • @mouradeljayi584
    @mouradeljayi584 2 ปีที่แล้ว +1

    Thank You so much for this great video ..please keep making videos on Laravel Testing !!!!

  • @muhammadmuneeb4122
    @muhammadmuneeb4122 2 ปีที่แล้ว +1

    Really appreciate your hard work, learnt so much, thanks alot 👍

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Happy to hear that Muhammad, thank you.

  • @atiqurrahman4611
    @atiqurrahman4611 ปีที่แล้ว +1

    Nice tutorial. Learned a lot, understanding of tests for beginner. Thanks for sharing.

    • @codewithdary
      @codewithdary  ปีที่แล้ว

      Thank you for watching Atiqur!

  • @GergelyCsermely
    @GergelyCsermely 2 ปีที่แล้ว +1

    Thanks, It is excellen to get code examples too. Thanks.

  • @olawaleesan8262
    @olawaleesan8262 2 ปีที่แล้ว +1

    Wowwww, I really love your videos..Thank u very much.Looking for more Laravel video

  • @AkhmadRizkii
    @AkhmadRizkii 2 ปีที่แล้ว +1

    This video help me to learn UnitTest easily. Thanks Dary

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Love to hear that Akhmad!

  • @asim-gandu-phenchod
    @asim-gandu-phenchod 2 ปีที่แล้ว +1

    Great stuff as always

  • @yohanvelho
    @yohanvelho 5 วันที่ผ่านมา

    Awsome video, very well explained!

  • @two-zero
    @two-zero 2 ปีที่แล้ว +1

    Thank you for this tutorial!

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Thank you for watching :)

  • @nhatkhanhtran111
    @nhatkhanhtran111 ปีที่แล้ว +1

    This video help me a lot, thank you so much😄

  • @TristanBailey
    @TristanBailey 2 ปีที่แล้ว +1

    Good clear starter video for testing

  • @houssemflutter2722
    @houssemflutter2722 2 ปีที่แล้ว +1

    Great tutorial , realy you are the best .👌👍

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว +1

      You are the best Houssem!

  • @joshbarros1995
    @joshbarros1995 2 ปีที่แล้ว +2

    Wonderful! I love your videos! Please make some about GraphQL (Lighthouse)

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว +1

      Thank you Josue! I'll keep that in the back of my mind! :)

  • @anuradhawithanage9861
    @anuradhawithanage9861 2 ปีที่แล้ว +1

    Thanks a lot, really helpful

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      You’re welcome Anuradha! :)

  • @hayder775
    @hayder775 3 หลายเดือนก่อน

    Thanks for the helpful video

  • @techsuniversity7386
    @techsuniversity7386 2 ปีที่แล้ว +1

    Great work 💙👍

  • @carlosf.2927
    @carlosf.2927 ปีที่แล้ว +1

    Great video. Thanks

  • @sumayazaman8862
    @sumayazaman8862 2 ปีที่แล้ว +1

    Thanks a lot. It helped me .....

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Glad to hear that Sumaya!

  • @TheJimboCOD
    @TheJimboCOD 2 ปีที่แล้ว +5

    Great video, helpful as always. Would you think about doing a series on building a REST API in laravel? I would absolutely love that

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      I have a crash course available: th-cam.com/video/xvqPEEpRBJ4/w-d-xo.html

  • @devops6414
    @devops6414 ปีที่แล้ว +1

    love it. waiting for new one

  • @tomjerry-nx9dl
    @tomjerry-nx9dl 2 หลายเดือนก่อน

    suppose i have a simple post crud. now my question is how to determine what to test? if i test each validation of a suppose store post, there may be more than 10 validations, then we have to create > 10 test methods? plus there are all these test in this videoes. making the all these test will be like creating the whole project again?

  • @kasidissjumrus3397
    @kasidissjumrus3397 2 ปีที่แล้ว

    Thank you for greate video
    PS. 13:00 This UserTest class was created with --unit option then Laravel extend from PHPUnit\Framework\TestCase. Without --unit, it will extend from Tests\TestCase.

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Didn't I mention that? Might have forgotten! Thank you anyways! :)

    • @albarranaufalaerdanto8502
      @albarranaufalaerdanto8502 2 ปีที่แล้ว +2

      Yeah i think it supposed to use UserTest inside Feature folder, right?

  • @FFarenco
    @FFarenco ปีที่แล้ว +1

    Your content is awesome! Make please a tutorial video with testing on Symfony if it's possible)

    • @codewithdary
      @codewithdary  ปีที่แล้ว

      Thank you! Definitely something I would love to do.

  • @saraelotmani5570
    @saraelotmani5570 2 หลายเดือนก่อน

    maaan thank you 💛

  • @nbahighlights2876
    @nbahighlights2876 2 ปีที่แล้ว +1

    Hi, Dary, I came across your channel, when I tried to find a Sanctum puzzle. The puzzle is when using Sanctum, it will generate a token, then send a request with the correct token to backend, it will work, it is great. But if I modify the token to make the wrong one, then send a request with the postman, it won't show any error, it will redirect to login page directly. So do you know how show an error message when send a wrong token to the backend?

    • @leinyuyetiane
      @leinyuyetiane ปีที่แล้ว

      This is late but in case any one still needs it, in your middleware, you should specify the 'auth:sanctum' middleware for your api

  • @shinyakoizumi601
    @shinyakoizumi601 ปีที่แล้ว +1

    Thanks for the video what do you use to show current path in the terminal?

  • @codewithdary
    @codewithdary  2 ปีที่แล้ว +3

    LETS CONNECT THROUGH INSTAGRAM.
    instagram.com/codewithdary

  • @amirtardasti39
    @amirtardasti39 ปีที่แล้ว

    This is an easy video to learn laravel test

  • @GiaNebieridze
    @GiaNebieridze ปีที่แล้ว +1

    Movie is beautiful. This scene is phenomenal. I was feeling every grams of my body during this scene. My heart was broken and my stomach was hurting.

  • @phpisdead
    @phpisdead 2 ปีที่แล้ว +1

    best video about testing , you have only one video about it?

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Yes! I'll be making more soon :)

  • @jokovBG
    @jokovBG 8 หลายเดือนก่อน

    Didnt get the delete user method :( Why we check if a user exists when we always create one before that ? And why at the end we hardcode assertTrue(true), isn't that way the test never possible to fail?

  • @AMoktar
    @AMoktar 2 ปีที่แล้ว +1

    Thanks bro

  • @haythemdridi8564
    @haythemdridi8564 ปีที่แล้ว +7

    13:05 its not a bug , you're writing test in the Unit folder , to test http requests you must write tests inside the feature folder

  • @geraldterencio8635
    @geraldterencio8635 2 ปีที่แล้ว

    Is it possible to execute the command in CMD because Im using sublime as my texteditor

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Yes definitely. You can also install an extension named “terminus” which gives you a CMD in sublime text

  • @Saleh2
    @Saleh2 2 ปีที่แล้ว

    if I want to test function or Controller I already created did i need to make php artisan make:test UnitTest and copy my function in it ? and same question with test database

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว +1

      You should be verifying responses in tests, and not the entire controller you created.
      If you are using the database, you should check if the right access methods are triggered.

    • @Saleh2
      @Saleh2 2 ปีที่แล้ว +1

      @@codewithdary
      thank you 👍🏼

    • @Saleh2
      @Saleh2 2 ปีที่แล้ว +1

      @@codewithdary
      I have problem with http test when I try it it give this error Failed asserting that two strings are equal. and aim an arrow to response->assertRedirect('/signin');

  • @chocinspired
    @chocinspired 2 ปีที่แล้ว +1

    Please any idea why my php artisan test gives this? I am using Lumen.
    >Command "test" is not defined.
    >Do you want to run "make:test" instead? (yes/no) [no]:

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Try running composer dump-autoload in case you upgraded recently.

    • @chocinspired
      @chocinspired 2 ปีที่แล้ว

      @@codewithdary just did that but same thing

  • @nicolasromero6144
    @nicolasromero6144 11 หลายเดือนก่อน +1

    Very nice content, if you have tine to upload a video with pest and laravel, anyway thanks

    • @codewithdary
      @codewithdary  11 หลายเดือนก่อน +1

      Definitely something I will be doing in the future!

  • @jonmason4127
    @jonmason4127 2 ปีที่แล้ว +1

    I thought regression testing was making sure existing functionality doesn't break when new functionality is released. Hence the term "regression".

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Regression testing basically perform after completing of retesting. The main purpose of regression testing is to check the impact of modification. Whether still our application is acting stable.

  • @2010sparsh
    @2010sparsh ปีที่แล้ว +1

    Can it do functional testing ?

    • @codewithdary
      @codewithdary  ปีที่แล้ว

      Yes, PHPUnit can be used for functional testing in PHP. It allows for the creation of automated tests that simulate user interactions with the application and can test the application's functionality as a whole.

  • @prezire
    @prezire 2 ปีที่แล้ว +1

    I noticed feature tests use the same DB during tests. is this correct? or should there be another separate DB to run tests?

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      It is yes, you need to test functionalities that interacts with the database to make sure that your app works on production

    • @prezire
      @prezire 2 ปีที่แล้ว

      @@codewithdary that's odd. i think a better approach is to clone the existing DB schema and append _test to the DB name. then delete that DB after the tests are done

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      But why? You won't actually change data because your just writing tests.

    • @prezire
      @prezire 2 ปีที่แล้ว

      @@codewithdary but there's an option for data persistence during tests, right? for example i want to test the relations. i would send API json with a mix of bad and good test data, and i want to count that only good data are the ones that are actually saved, right? i could be wrong but other frameworks use a cloned DB for tests and then drops that DB once the tests are done, so your current DB is untouched. in laravel, it's like setting the flag --env=.env.testing, using a test DB. although other frameworks does it implicitely.

    • @pauldrage9931
      @pauldrage9931 ปีที่แล้ว

      @@codewithdary I'd ordinarily agree with you - and I think you've done a great job putting this video together - but you are saving data to the db at 20:04 ( th-cam.com/video/UjA-16diixc/w-d-xo.html ) . Laravel has a doc on testing an it does state that it will auto create a testing db and tear it down after tests..

  • @LazyJoeyDesigns
    @LazyJoeyDesigns 2 ปีที่แล้ว +1

    hi dude, I believe that there is an error in the code you wrote for the user delete testing.. you are creating the user with the factory, then checking if the user exists with an if statement.. but in case that statement won't be true, the test will assert true regardless.. I'm not sure if you get what I mean.. as far as I know test shouldn't contain any if statements ?!

  • @pawemieleszkiewicz1602
    @pawemieleszkiewicz1602 2 ปีที่แล้ว +1

    What is your ZSH theme?

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      I have a complete video where I setup my terminal! It's under the Laravel video series.

  • @ramziddinrustamov5221
    @ramziddinrustamov5221 2 ปีที่แล้ว

    Hi , i have questions about testing ! Why it is important testing.

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว +1

      Hi Ramziddin, I found a pretty interesting comment on the internet about testing:
      what do you do when you write code without tests? after almost every change you run the whole application, you wait until it starts, you go to the page you've just changed, you input some data, you press the button and you check... if the answer is correct, if the popup appeared, if the mail was sent and so on. you DO test. but you just do it manually. and you repeat all those tests after every change. and if you don't repeat them then you will find bugs on production and you will have to correct them quickly under stress and you will have to again check if you've fixed them correctly. and you will have to do it manually...
      Hope this makes sense.

  • @shanmugapriyam3163
    @shanmugapriyam3163 2 ปีที่แล้ว +1

    Part2 - Laravel unit testing video link please ?

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      There is no part two. Probably in the future :)

  • @victorhugosens4443
    @victorhugosens4443 2 ปีที่แล้ว +1

    you are legend

  • @sagar8234
    @sagar8234 2 ปีที่แล้ว +1

    "Command test is not defined" while using php artisan test.

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      Try running composer dump-autoload in case you upgraded recently. Otherwise try to delete your vendor directory and try running composer install to see if that helps

  • @aruppaul1468
    @aruppaul1468 2 ปีที่แล้ว

    please make laravel react based project

  • @dfordemo981
    @dfordemo981 2 ปีที่แล้ว

    i came here from your github

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว +1

      That’s awesome! Love to hear that :)

  • @m7mmadomar
    @m7mmadomar 2 ปีที่แล้ว

    What is your theme 👍👍👍👍

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      I installed a extension called Material Theme, it was one of the options!

  • @danialhajirajabi4286
    @danialhajirajabi4286 2 ปีที่แล้ว +1

    not complete for a tutorial. but good for beginners. 🙏

    • @codewithdary
      @codewithdary  2 ปีที่แล้ว

      In depth testing will be coming soon! :)

  • @lijinv674
    @lijinv674 2 ปีที่แล้ว +1

    👍

  • @webspot17530
    @webspot17530 2 ปีที่แล้ว

    Not very beneficial