CS50P - Lecture 5 - Unit Tests

  Рет қаралды 257,471

CS50

CS50

Күн бұрын

Пікірлер: 125
@SyphriX
@SyphriX 2 жыл бұрын
Happy to see unit testing making its way into the curriculum. I’ve been a software developer for 7 years now and this is one of the largest knowledge gaps we have with new devs. Test early and test often! Your designs will be better for it. And if your code is hard to test it often means you haven’t sufficiently broken down the problem into its isolated sub problems.
@BaldJewishBastard
@BaldJewishBastard 2 жыл бұрын
If you dont mind me asking, why are you watching this course if you are already developer for 7 years?
@Qwonk
@Qwonk 2 жыл бұрын
@@BaldJewishBastard Gotta stay learning. Tech changes and it's nice to have a refresher
@zyada9334
@zyada9334 2 жыл бұрын
@@BaldJewishBastard being a developer for 7 years doesnt mean he learned all languages. This is not "programming" course this is "python" course
@SyphriX
@SyphriX 2 жыл бұрын
@@BaldJewishBastard fair question. Mostly because I’ve learned that relearning the basics several different ways is key to leveling up. I’m also curious about the state of computer science education and where it’s heading; the cs50 family of courses seem to be a great bellwether for that. And lastly, because cs50 kickstarted my own education back when I took it through iTunes university back in 2011. And since I couldn’t afford to attend university back then, I owe my career to free online university courses like this one. So I like to check in on them over time to see how they’ve evolved.
@mehmedkukavica8076
@mehmedkukavica8076 2 жыл бұрын
I graduated from the computer department at the technical college 35 years ago and we had half a math library for the problem set. We learned how to solve scientific problems with the help of computers. Now that's extreme programming. First, do the tests analytically, and then solve them numerically.. In the 1990s, it was time to program in any way. If there are errors, it's not a problem, we'll fix them when we update. That's why in the USA there are more f35s on the ground than in the air. I made programs that were 15 lines long and ran for 20 minutes on mainframe computers in the eighties. Only a computer can do those calculations. Decisions are made based on the calculations. It seems to me that technology has caught up with me. David is the generation of the nineties, the generation of the Internet, databases, information transfer: text, images,...
@Flarui
@Flarui 2 жыл бұрын
the teacher is incredible, so much enthusiasm and energy! I understand his lectures much better than those of my teachers who mumble about half asleep and disinterested :(
@domemvs
@domemvs 2 жыл бұрын
I agree. And he has been that way already years ago without any of these traits degrading.
@liya2428
@liya2428 5 ай бұрын
00:08 Writing our own tests to ensure correct solutions 02:20 Testing unit tests and controlling main function execution 06:13 Testing code functionality using unit tests. 08:08 A mistake in the square function caused a test to fail 12:11 Introduction to using try and except in Python for error handling 14:16 Writing comprehensive unit tests helps catch discrepancies in code logic 17:51 Unit testing is a formal way of testing individual function units. 19:37 Using pytest to automate the testing process 23:13 Testing user input for different data types. 25:04 Unit testing and its importance in controlling program's output 28:39 Divide tests into separate categories for better debugging insights 30:26 Identifying and fixing failing unit tests 34:01 Unit testing frameworks automate the process of catching exceptions, making testing more efficient. 35:54 Testing for type error by deliberately passing a string to the square function. 39:43 Testing a function with arguments and return values 41:38 Functions should either return a value or have side effects, not both. 45:26 Writing multiple tests helps in identifying bugs more easily 47:25 Keep tests simple and small to avoid complexities and flaws.
@ansumanroutray1118
@ansumanroutray1118 Жыл бұрын
it is just amazing how this teacher, teaches complex topics of easily, that is easy to understand.
@cleidysbrooks
@cleidysbrooks 2 жыл бұрын
Good Morning Python Family. I like this lectures very good teaching. David you are the best!
@حسنين-ذ6ل
@حسنين-ذ6ل 7 ай бұрын
you have to name the file you want to test with "test_(anything)" in order to use pytest this also applies for all the function inside that file
@yqhanliving
@yqhanliving 10 ай бұрын
So useful to learn unit test and structuring the code with a testable design. Thank you cs50 team!
@MrTomro
@MrTomro 2 жыл бұрын
amazing! the teacher is so enthusiastic and i watched this whole thing in one go without any problem
@njmanga617
@njmanga617 2 жыл бұрын
Love your lectures i completed the cs50 to python and loved it, it was well worth the time and effort
@mwlk9508
@mwlk9508 2 ай бұрын
amazing so far. I learnt so much things so far. I did many programming courses in the past. But this the best one that actually teach me the concepts properly.
@williamenur3404
@williamenur3404 Жыл бұрын
My progress is awesome thank CS50
@RGen-z
@RGen-z 5 ай бұрын
Legendary quality🙏🏾 Thank you so much for the opportunity to learn
@falaknaaz7569
@falaknaaz7569 26 күн бұрын
awesome lectures
@evachen8623
@evachen8623 Жыл бұрын
Thank you Daivid as always~🥰
@rounakpatra2210
@rounakpatra2210 2 жыл бұрын
It will be great if cs50 provides basic of DSA using python
@bigbro1815
@bigbro1815 7 ай бұрын
Thanks David
@eashwaransridhar252
@eashwaransridhar252 Жыл бұрын
For folks who are facing an issue 20:42 to run pytest sting an issue "pytest iis not recognized as an internal or external command " You can use the command : python -m pytest "Test file name"
@Tomharry910
@Tomharry910 Жыл бұрын
It worked. Thanks!
@ElliotWayne4401
@ElliotWayne4401 8 ай бұрын
when i write , from (name of the file) import function ,it tells me that it cant read the name of the file
@eashwaransridhar252
@eashwaransridhar252 8 ай бұрын
@@ElliotWayne4401 Check witht the permission of the file ,Can it be acessed to other user also ,In windiws its a headache but in linux its prettty simple
@ziyaadmallick9192
@ziyaadmallick9192 4 ай бұрын
It worked but what's the reason behind it saying it doesn't recognize pytest as a cmdlet, function etc
@sushmasingh9040
@sushmasingh9040 2 ай бұрын
you are the greatest person
@rifkiardiansyah616
@rifkiardiansyah616 2 жыл бұрын
The best teacher
@mahipalmahato7648
@mahipalmahato7648 Жыл бұрын
4:23 prog1 9:37 prog2 12:15 prog3 16:06 prog4 19:58 prog5 29:59 prog6
@RemotHuman
@RemotHuman Жыл бұрын
Wouldn't it be easier to make a dictionary or a list of tuples with input and expected output to a function, and then call all of them with a for loop and print out which ones failed? That way you are repeating yourself less (writing the name function you are testing, writing assert, writing separate functions for categories). You just have to write the inputs and outputs in a dictionary like this { ("input param", "input param2") : "expected output", ... }
@panos.fasoulas
@panos.fasoulas Жыл бұрын
strange to me that this question is not answered. your question to me as a beginner is a question i d like to know the answer.
@jgojiz
@jgojiz 5 ай бұрын
thank you! 💖💖
@martinp7841
@martinp7841 2 жыл бұрын
These lectures are nice but why is the video screen SOOOO bright? I need sunglasses to watch.
@justmantate
@justmantate Жыл бұрын
cause it’s green screen I guess
@ethanrojas2270
@ethanrojas2270 Жыл бұрын
Or because 😮 Malan is super bright 💡
@Ancient-Africa
@Ancient-Africa 4 ай бұрын
Lol... The person who made devices brightness adjustable: Am I a joke to you😂
@mahmutpekkara
@mahmutpekkara 6 сағат бұрын
44:33 assert added to the line 5
@cs50memepage2
@cs50memepage2 2 жыл бұрын
Don't try to solve serious matters in the middle of the night.
@novacoax
@novacoax 2 жыл бұрын
I do that all the time 😂
@nomelastname3771
@nomelastname3771 2 жыл бұрын
@cs50 I understand what u mean, and i agree. Don't do important things when u are tired. It will be ease to forget something or other bad thing
@sayori3939
@sayori3939 2 жыл бұрын
If you have the actual solution then go ahead if you're just assuming things better leave it to the next day
@NikiruSan
@NikiruSan 4 ай бұрын
I did that, and now It's already morning.😢
@LAAL0_O
@LAAL0_O 5 ай бұрын
can't wait for the exercise to be 100x harder than this lecture.
@jonnysegway7866
@jonnysegway7866 3 ай бұрын
this is the way!
@オズ-o8p
@オズ-o8p 2 ай бұрын
44:41 don't forget to add "assert" in your code guys
@mahipalmahato7648
@mahipalmahato7648 Жыл бұрын
start at 36:02
@fslurrehman
@fslurrehman 2 жыл бұрын
Instead of try , shortcut is to use comma and then error message string in front of assert.
@sayori3939
@sayori3939 2 жыл бұрын
Gimme a code example :0
@tacy91
@tacy91 Жыл бұрын
@@sayori3939 assert square(3) == 9, "3 squared was not equal to 9"
@sayori3939
@sayori3939 Жыл бұрын
@@tacy91 huh neat
@Yukishuru
@Yukishuru 2 жыл бұрын
Nice
@AniruddhaKarmakar-r6t
@AniruddhaKarmakar-r6t 11 ай бұрын
@evylrune
@evylrune 2 жыл бұрын
"pytest calculator_test.py" in the terminal gives me: "The term 'pytest' is not recognized as a name of a cmdlet, function, script file, or executable program." I did install and update it with pip (Python and visual studio code are also installed in their default installation folders). Then I found this online: "python -m pytest" which does run the test on my file fine... Is there a way to make the normal pytest command work?
@doc_parallex6139
@doc_parallex6139 2 жыл бұрын
you have to install pytest to be able to use it so in the terminal type: pip install pytest hope that helps
@sayori3939
@sayori3939 2 жыл бұрын
@@doc_parallex6139 i installed a ransomware and now i am being hold captive after writing that command :/ I'm kidding :3
@vishnubhat2084
@vishnubhat2084 Жыл бұрын
nice
@destroyer980
@destroyer980 2 жыл бұрын
I know nothing about coding and i am watching this 😅
@novacoax
@novacoax 2 жыл бұрын
😂
@mariamorozova5360
@mariamorozova5360 2 жыл бұрын
I was going to watch a movie but this stream changed my plans
@novacoax
@novacoax 2 жыл бұрын
@@mariamorozova5360 you'll enjoy it. David J. is a great lecturer
@mariamorozova5360
@mariamorozova5360 2 жыл бұрын
@@novacoax yeah he is! I wathed CS50 already and now this course
@spht9ng
@spht9ng 2 жыл бұрын
Watch the first one in the series and learn a new skill. it's quite a bit of fun
@anismadrid2645
@anismadrid2645 Жыл бұрын
The fact that only few people watch this is so sad 😶 Thanks world
@chrislachman2010
@chrislachman2010 9 ай бұрын
It's not sad, it's less competition.
@faith231q
@faith231q 2 жыл бұрын
For the problem set5, I'm getting the error in Refueling. It says: :( correct fuel.py passes all test_fuel.py checks expected exit code 0, not 2. Someone please help why am I getting this error. Would really appreciate your help
@kumaraperumal
@kumaraperumal Жыл бұрын
maybe you used sys.exit(2) insted of sys.exit(0)
@حسنين-ذ6ل
@حسنين-ذ6ل 7 ай бұрын
this might help :- try naming the file you want to test with "test_(anything)"and then use pytest this also applies for all the function inside that file
@jaytep5647
@jaytep5647 3 ай бұрын
44:31 Line 5 of test_hello.py is missing the keyword assert, this is fixed when the camera switches but still worth pointing out
@milorodval678
@milorodval678 Жыл бұрын
When I test the "hello.py" it's everything OK, but when I run it with "python hello.py" and I don't pass a name it does not give me the default (that should be "world"). It gives me nothing.
@bartoalheiros3616
@bartoalheiros3616 9 ай бұрын
I'm using Python 3.12.1, pytest-8.0.0, and, when I do the check ->> hello("David") == "hello, David" with the print( ) function on the hello( ) function like this: def hello(to="world"): print("hello, ", {to}) that pass.
@srisaisubramanyamdavanam9912
@srisaisubramanyamdavanam9912 2 жыл бұрын
Would this course will be easy for those who taken already cs50 introduction to computer science course
@gojosensei6501
@gojosensei6501 2 жыл бұрын
Obviously are u stupid
@sayori3939
@sayori3939 2 жыл бұрын
Idk, this is easier than cs50x but some problems sets have some obscure solutions or I'm the one obscure ._. but I've already come this far, cs50x took me some months
@ethanrojas2270
@ethanrojas2270 Жыл бұрын
46:15 dr.malan “if or oh of “
@oyekanmibamidele6196
@oyekanmibamidele6196 2 жыл бұрын
I'm aiye I'm alive l can't deny this...Peyton is beautiful to know.
@sayori3939
@sayori3939 2 жыл бұрын
Who's "Peyton"?
@fredoverflow
@fredoverflow Жыл бұрын
​@@sayori3939 Simon Peyton Jones?
@billionairesbio
@billionairesbio 2 жыл бұрын
HELLO... ..THIS IS CS50 ....
@souvikmoitra6700
@souvikmoitra6700 Жыл бұрын
I have a question when two of the functions failed, and one passed but then also the 100% test failed was encountered but it should be 66.66% right ??
@BhavyamMittal-gi3jn
@BhavyamMittal-gi3jn Жыл бұрын
I tried doing the pytest but this is what it tells me: "The term 'pytest' is not recognized as a name of a cmdlet, function, script file, or executable program." I did install it with pip (Python and visual studio code are also installed in their default installation folders). How do I make it work?
@sayori3939
@sayori3939 2 жыл бұрын
Aawwwn whenever i reach the end of a lesson i feel a little bit empty :/
@benc7910
@benc7910 2 жыл бұрын
what vscode theme is this ?
@MightyBaja
@MightyBaja 8 ай бұрын
Okay the unit test exercises are are hard... check50 just gives yellow checks on my pytest
@allbusiness5402
@allbusiness5402 3 ай бұрын
48:14 Whats mkdir test?? And what does code test/test_hello.py mean? Did I miss something?
@umair5807
@umair5807 2 жыл бұрын
Sir, is there any book for CS50 for exercises ?
@StreamAod
@StreamAod 12 күн бұрын
i am having a problem in implementing unit test ,because i dont know why but in my cs50 compiler I am not able to import my functions from actual code file to test file . please if somebody can help me with this .
@umair5807
@umair5807 2 жыл бұрын
23:15 Sir I got an error, when I type "pytest test_calculator.py" in terminal. I got this error: The term 'pytest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Im using pyCharm. what's the problem sir?
@BrendaAnderson
@BrendaAnderson 2 жыл бұрын
You will have to install it. www.jetbrains.com/help/pycharm/pytest.html#enable-pytest (if the link doesn't work, google pytest in pycharm and it should lead you there.
@GrimmReaper3185
@GrimmReaper3185 Ай бұрын
Then more i learn the harder this gets. should 'nt it be easier?
@XTremeTerrorSquad
@XTremeTerrorSquad Жыл бұрын
Hi, did anyone used free certificate in their resume?
@koshobai
@koshobai Жыл бұрын
There's definitely no harm in it. I even put my GitHub profile on my resume.
@appleapple1763
@appleapple1763 2 жыл бұрын
35:03 Am I the only person that the float like 1.2 works in this code? assert square(1.2)==1.44 it works totally fine for me
@fredoverflow
@fredoverflow Жыл бұрын
assert square(0.1) == 0.01
@rikmoran3963
@rikmoran3963 Жыл бұрын
Did you not listen to his explanation? He said that he wasn't testing for floats (which of course could have many decimal places) because the result may include rounding which makes it harder to assert the correct answer. He clearly wasn't referring to something as simple as 1.2. He was only testing the expected input.
@danho2441
@danho2441 2 жыл бұрын
The __name__ variable isn’t well explained.
@ethanrojas2270
@ethanrojas2270 Жыл бұрын
I know
@hasty-prize9900
@hasty-prize9900 10 ай бұрын
@@ethanrojas2270 without the __name__ function in this video if you were to import a function from another file , when you run it main will automatically run with it.
@huwrw007
@huwrw007 Жыл бұрын
15:00
@bluegrapes2010
@bluegrapes2010 Жыл бұрын
AT 4:22, what does if __name__ == "__main__": mean? can anyone explain to me? Thanks,
@jamessadler851
@jamessadler851 Жыл бұрын
It's incase your code is imported into another python file
@bluegrapes2010
@bluegrapes2010 Жыл бұрын
@@jamessadler851thanks!
@demonking1063
@demonking1063 2 жыл бұрын
remove hdr
@sayori3939
@sayori3939 2 жыл бұрын
Why? In my old computer they look fine and my phone too...
@koshobai
@koshobai Жыл бұрын
Yes HDR is here to stay.
@HungryTv13
@HungryTv13 Жыл бұрын
47:47
@ThomasKroll-ch7nb
@ThomasKroll-ch7nb Жыл бұрын
The lectures are great, but check50 is a joke. Check50 returns results which are inconsistent with pytest and only returns vague errors like "expected exit code 0 not 1" which can not be duplicated outside of check50. Harvard should be embarrassed to publish a learning tool this unreliable.
@qqmayakashi
@qqmayakashi Жыл бұрын
why did the result at 37:53 passed? it should be failed as we must only accept integers at function square
@qqmayakashi
@qqmayakashi Жыл бұрын
everytime i instead use the with pytest.raises(ValueError) instead of TypeError, i always catch the error but the pytest shows it catches typeError. But if i use TypeError, it does not catch any anomalies on square("cat")
@rikmoran3963
@rikmoran3963 Жыл бұрын
I think you missed the point. He is telling the test program that if someone enters a str value into the square() function it will generate a TypeError error. When the test program runs it trys the string "cat" and does indeed generate a TypeError. This is expected behaviour and therefore correct. We are testing the square() function, not the main() function from the module which is the one that handles the input.
@brianholmes1655
@brianholmes1655 2 жыл бұрын
I appreciate that these videos are in HDR, but having a predominantly white background that is > 200 nits (at least) makes the videos essentially unwatchable at night and an unnecessary drain on battery during the day.
@RoccoCampana
@RoccoCampana Жыл бұрын
@@mondo_burrito Sometimes you just gotta look on the bright side.
@hernandezlopezmiguelangel276
@hernandezlopezmiguelangel276 Жыл бұрын
Hi to everyone in the minute 23:15 I got an error, when I type "pytest test_calculator.py" in terminal. I got this error: The term 'pytest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. I'm using VS code. does anyone knows how to fix the problem?:(
@davidjmalan
@davidjmalan Жыл бұрын
Be sure to use code.cs50.io !
@jacquessantiago
@jacquessantiago Жыл бұрын
You'd have to "pip install pytest" first
@24bit192khtz
@24bit192khtz 4 ай бұрын
The false American accent by those Desi guys who are asking questions, is quite irritating!
@dhruvrathod32
@dhruvrathod32 4 ай бұрын
ur here to learn, buffoon!
@atasagun1437
@atasagun1437 Жыл бұрын
boring
@sayori3939
@sayori3939 2 жыл бұрын
WARNING: this problem set has a. Lot of BS specially that "refueling" one. If you write the test code "correctly" but doesn't use that quirky pytest.raises, check50 gives you the absolute most nonsense message and doesn't complete any check, if you indid use that pytest.raises but in the fuel.py code catch the ValueError, ZeroDivisionError inside a try block check50 passes but pytest complains saying DiD NOt RaISe ZeroDivisionError OBS: if you explicitly *RAISE* those exceptions inside a try via "if" or whatever it eill not detected it, you have to use the ifs/raise outside any try blocks 😠😠😠 tht made me so angry!
@MightyBaja
@MightyBaja 8 ай бұрын
Faced the same problem...had to submit it because it's been days trying to figure it out
CS50P - Lecture 6 - File I/O
1:32:25
CS50
Рет қаралды 316 М.
CS50P - Lecture 7 - Regular Expressions
2:05:00
CS50
Рет қаралды 279 М.
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 30 МЛН
Каха и лужа  #непосредственнокаха
00:15
Python Tutorial: Unit Testing Your Code with the unittest Module
39:13
Corey Schafer
Рет қаралды 1,3 МЛН
7. Testing, Debugging, Exceptions, and Assertions
41:33
MIT OpenCourseWare
Рет қаралды 182 М.
CS50P - Lecture 4 - Libraries
1:17:29
CS50
Рет қаралды 299 М.
CS50P - Lecture 3 - Exceptions
44:36
CS50
Рет қаралды 319 М.
How To Write Unit Tests For Existing Python Code // Part 1 of 2
25:07
Effective Unit Testing by Eliotte Rusty Harold
48:10
Devoxx
Рет қаралды 104 М.
CS50P - Lecture 9 - Et Cetera
2:29:04
CS50
Рет қаралды 221 М.