Learn C# with CSharpFritz - Ep10: SDK, Project Types, and Testing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey there we go good morning good afternoon good evening whatever time it might be where you are out there welcome back to the live stream my name is jeff fritz and today today we're going to be writing a little bit of code and this is c sharp with c sharp fritz we write some code here we have a little bit of a discussion we talk and we learn we get into the beginnings of c sharp and this is episode 10 of uh of an ongoing series that we started way back in september learning about c sharp getting into the basics building our first code and today we're going to start writing our first tests building our first solution files and and working on projects together really looking forward to this um let me say hello let's let's say hello to uh the folks here in the chat room they're over here hello hello uh shy sharp is here viral flows lithics good evening to you hello to you viral flows after a long time always great to have folks come back and tune in um these videos are archived they're over on the youtube channel and of course you can watch us live mondays 9 a.m eastern 6 a.m pacific and now with the time change the where what is it 1400 utc you can also find us over here um coated beard is here pro bricks black eye betting course hello to you bettencourse good morning good morning good afternoon coated beard john mazza is here hootenheimer good morning good morning to you um so this is the first of of um [Music] this is going to be our first episode where we're going to step outside the browser we're going to get into actually writing code on disk and i'm going to focus on using visual studio code today but we're going to learn how to start using our tools how to start putting together more than just a couple lines of c sharp code here and there where we're going to start to get a little bit more productive in writing code that compiles together that we have to compile evaluate and take appropriate steps on on our machines or on other folks machines if we're doing some sort of remoting operation but we're going to learn how to put those pieces together and start building our first our first solution and we'll talk a little bit more about the project here in just a little bit let me get some music playing in the background here this is stream beats by our friend harris heller this is music that's licensed free to listen to free for folks to use it's copyright free anybody can use it anywhere they'd like on twitch youtube all those places and today we're going to be playing the lo-fi playlist um a bunch of really groovy music that's it's just great to listen to in the background it's not going to interrupt us but it's going to help to fill some of the quiet space so that we can talk and and have some groovy music in the background there so this first song is called zen and we'll provide a link in the um in the youtube description below to harris heller's stream beats and you can listen to them while you're writing code documentation doing homework whatever it might be that you're working on so thank you so much harris we appreciate you sharing your music um and we'll listen to it here in the background as we're writing some code together um let me take a look here eeny meeny miny okay we're good so i've got a lot of notes for today i've got uh a lot of things that we're going to be going through and i wanted to do a bit of this live coding style see where our discussions take us and fill in the notes fill in the content appropriately because i am i'm doing a little bit of representing content from within the documentation if we decide not to go too far down there well we'll pivot and go in a slightly different direction that's that's part of the benefit of watching the live stream while we're here together being able to ask questions and help drive the direction for this you're already relaxed fantastic john mazza glad to hear it so um let's head over to to the code and take a look at our github talk about talk about what we're doing today and i want to give you a little bit of a hint of the schedule ahead for november so uh let's take a look over there let's head over to that gonna head over making my way over to to my desktop here we go and there this is our github repository c sharp with c sharp fritz let me turn on i have a command here that we can execute in the channel that you can execute and see [Music] get quick access to this repository there's there's the link and i'm going to turn on our commands over here that also give you access to the youtube channel not access to it but you know you'll be able to uh run a command and get a reference to it yes we're going to tell lame dad jokes today as well yes that's part of what we do is tell lame dad jokes okay maybe not always what we do but it's kind of a thing right um so let's um i'm keeping an eye on things here um i want to make sure i have that running properly good all right so here in the github repository this is where everything happens around the show all the things that we're doing all the things that we're going to be talking about issues questions that folks raise you can log them here in the issues tab and we'll address them if you have questions about the content that's going on inside of the project log them in the comments below if you're watching the youtube video or if you're here on twitch write them in the in the twitch chat and we'll get to those immediately and you'll see the messages pop up over there on the side the various questions that are asked during the live stream and there'll be links to those below so you can go back to them later if you need to reference so um typically i tell you we're gonna click through the launch binder link and run our jupyter notebooks we're moving away from that because we're getting into full solutions complete projects and for this we need to now level up our tools we need to get outside the browser and actually work with text editors and tools that are going to help to compile help to turn our source code into real applications and you're going to see in our first episode that that goes through that process you're going to see how we can figure and how we get that running today i do want to show you in our projects list um this is where you'll see my thinking and planning around what's coming in that future sessions we're focusing on unit testing today and as i got into writing about the unit testing topic i realized we need to talk about installing the.net sdk we need to talk about what a project is namespaces solutions so we're going to be covering that topic today next week is dot net conf we have that coming up um and along with dot net conf is the release of dot net five huge release new major version of all of the tools all of the frameworks available for you as a net developer as a c-sharp developer including c-sharp nine now i don't wanna cover c-sharp nine until it's released so what i want to cover on monday next week.net conf starts on tuesday on monday i think we should cover entity framework and talk about getting and interacting with data from an sql database and we'll particularly work with um sql lite but all of the things that we're going to be working with will also work with sql server postgres mysql oracle db2 choose your favorite sql database entity framework is a fantastic object relational mapper that sits in front of those databases and will help you write code to interact with them since we have.net conf tuesday wednesday thursday next week that's november 10 11 and 12. i hope you tune in we've got almost 70 sessions of amazing presenters and information about all the new things that are going on in dot net including 24 hours straight of folks from the.net community showing various community projects various tips and tricks and techniques that they've found success with as they've been going through the preview versions and now the release candidate of net 5 so make sure you tune in for any part of that uh day one and oh well day one is is eight to nine hours including an attendee party that we call a code party at the end of the day that i hope you tune in day one is all microsoft presenters teaching about all the cool new things that are happening including c sharp nine day two is technically from nine a.m to about 5 p.m pacific time that's what noon for folks on the east coast um and uh the 5 pm utc time now that's fine it's technically that long but at 5 00 p.m we kind of flip the switch and we go into community mode and for 24 hours straight around the globe speakers from every continent except for antarctica i couldn't find an arctic speaker this year um we have folks joining us every time zone is represented and you'll be able to learn all kinds of great things about.net in that 24 hour cycle so there's my my pitch about net conf but the mondayafter.net conf we're going to talk about c 9 features and dial those into our learnings here on the screen so next week entity framework the week after c sharp nine the week after that we're gonna start talking about user interface and it sounds like we want to start building our first blazer application so we've had an issue out here that folks have been able to click into and and vote on um it is down here user interface frameworks and i've asked folks to vote with a thumbs up on what they'd like to see oh actually it looks like asp.net core just surged to the lead here so you can click through to this issue vote within within emote thumbs up appropriately on which one of these frameworks you'd like to see us um as our discussion cover first and it looks like we're going to talk about asp.net core and then blazer maybe wpf or xamarin later so looks like we're definitely getting into a web framework here and that's what we will be learning about there's a question here from priyank in the chat room let me put this up which version of entity framework will will we be using um even though it's just before dot net conf in the full release of entity framework core five i will be using entity framework core and i won't be going down into any five specific features um but i will be using version just because it's here the release candidate is done at that time um it's going to be pretty well locked in so i won't cover any of the entity framework 5 features but we will be using entity framework core five um hey spadagus welcome in black eye with a follow-up question will i use any mapping tool like auto mapper similar no um i don't want to get into anything um any of those tools those are these are great tools these are great libraries that will help you with with managing interacting with your projects and moving things around they have a place i want to stay as simple as possible to show how to use some of these frameworks [Music] i won't get into these as part of that so they have their place but um i want to stay simple and focus on what you can accomplish with the frameworks so um so that's what i have planned that's what i have coming up in the next few weeks i i'm woefully behind on publishing a blog post about this series about the github repository and how this all fits together i also have a blog post that i've been writing about how i use jupiter notebooks as part of this so a little bit of work that i have to publish but coming soon all right um so let's talk about let's talk about getting started with net on the metal on the actual physical machine that you're using now this means you're going to need a laptop or a desktop um you can even work on a a chromebook um but you won't be able to work on a tablet or a phone directly you might have to you'll have to remote into another system in order to participate and and go through the samples follow the code that we're going to be working on today we're going to start building out a sessions folder here and this is um i'm calling this season zero one this is session ten um when we get into the the user interface framework uh i think we'll call that season two of this which lines up kind of nicely with the month of december so um this is session 10 we're going to talk about project solutions and unit testing if you'd like to follow the actual document that i'm working with here there's a link to the to the folder where the sample the the sample code is that i'm working with today it's actually it's not quite complete sample code it will be complete when we're done writing our code today but you can follow along you can scroll ahead if you'd like um i have a copy of this up on another screen over here so that i can see um exactly what's going on and follow my notes as as we're building and working together today um oh that's an interesting question uh is that polamic palomin is it paloman or palamin let me know i want to make sure i pronounce your names correctly um any chance of including a behind the scenes in the future well there's a green screen behind me uh what kind of information would you be interested in in learning about i i'd be happy to to we we did uh the the errata show to catch up on some of the missed topics um behind the scenes i'd i'd be happy to talk a little bit more about um how these streams are produced how some of the code is written um what would you be interested in let me know chat room go ahead and sound off there and if you're watching the recording if there's any production information that you're interested in learning about let me know in the comments below i think this is a fine idea we can we can definitely um talk about that write up a blog post include some video um lithics is even going so far in the chat room to say uh uh some bloopers i don't think i don't have bloopers i'm just the way we expect this to be uh i don't know um i haven't cut any content out of a stream um any of these these streams um because they're just the way we planned them um let me know i can think about that but yeah what would you like to see what would you like to learn about about re well what would you like to learn about how this is produced um i have i'm happy to show how how obs works how the obs the open broadcaster software that's what i use to assemble and produce the scenes that you see here um happy to to show a little bit of how uh the notebooks work how they're put together how my process works when when i go through and write the streams and the script for what we're working on so happy to to share that information with you paladin fox uh chimes in here um with some love from costa rica well thank you paladin fox i appreciate that um if folks want to sign off where they're from uh let me know costa rica that's amazing i'm on the east coast of the united states um and you're going to see some of our streams this week slow down or or not run here on the visual studio channel so they don't conflict too much with uh the american election and some of the other things that may be happening um black guys interested in some tools and gadgets to switch screens um oh yeah we can definitely talk about that northern virginia is where the giggles is all right cool another east coast of the states that's fantastic um so let's start talking about how you can get a copy of net get started building and writing applications we've been doing this like i said in notebooks but let's actually get let's actually get productive and get some tools on our system that we can work with job npa is in from costa rica the netherlands is mufana blue uh kyrgyzstan oh my goodness the fan of 30 stm oh yes we're gonna get into unit testing it's gonna we need to set up a little bit but we're gonna get into unit testing and talk about some practices and some practical ways that we can do that spain we've got some folks watching from 191188 cool very cool all right wales is where better call sean is from see look at this fantastic and uh lithics is dialed in from perth australia oh my goodness that's awfully late the.net con after party starts at 1 am for you no it's it's in the morning actually st petersburg russia is for lucky ducky oh my goodness thank you wisconsin is where mr demon wolf is hello hello um so let me show you where you can go to get your copy of net so um real easy get dot net literally um it's it looks like a cheesy domain name but there it is there in the chat room i'll copy and put this in the link in the youtube description below but get dot dot dot net and it will take you to this site where you can choose whether based on whatever operating system you're on you can choose to download and install either.netcore.net framework this will be updated on november 10th when dotnet 5 is released november 10th 2020 you might be watching this sometime in the future and it might not look like this because this dotnet release candidate for net 5 will be launched and you'll be able to download it down here appropriately now we're building apps we're not just running applications so you need to download well you could download the dot net core sdk for net core 31 but i'm using dot net 5 so i'm going to download the release candidate now for those of you that are watching this before this is released whenever you see a release candidate from from the dot-net team it typically inc includes what we call a go live license that's assurance to you that you're going to receive support that you can start working with this the api the contents of it aren't going to change significantly there might be some minor changes but they're not going to change significantly as it gets ready for the final release the rtm we used to call that going gold the release to manufacturer now it's released to the web really because it's being published for folks to download so you can download this sdk and in order for you to maintain support you've got to install the latest versions of this as it becomes production ready as we get to that rtm that's not too hard to consider if you're building and testing and you want to try out the release candidate version so whether it's net5 net 6 other versions of net in the future you'll have that same experience and there's links um to the support cycle should be a link down here to support you can click through and learn more about the support cycle for it if that's something that you and your organization is concerned about it's pronounced pa lamen okay fantastic so real easy to download and get this started depending on what operating system you're on there's various installers there's binaries that you can download and extract place appropriately on on your machine so that you can interact with that for this machine that i'm working on this is a windows machine i've already downloaded the installer for uh for net five and i have it running on my command line i'm using windows terminal and it's already here waiting for me to interact with um [Music] right here let me um [Music] yeah that's fine i can execute and interact with it once i have it installed i can run dotnet info and it will tell me well here's the version of the sdk that i'm running dot net dash dash info and it'll tell me well this is a windows machine this is the windows version that i see that's interesting i thought i installed the 2040 no matter um the uh runtime id this is an x64 windows 10 we'll learn more about runtime ids later um and exactly where this is installed on my machine there's other sdks that are installed including the 31403 so i can work with and do things with um older well the current supported version of net for me right now uh november 2nd 2020 is 31403 uh but you can see i have other.net runtimes installed including 2.1 um 3.1 i have two different minor versions of 3.1 and there's there's the 50rc2 so this is the release candidate version that'll help me write asp.net core applications those are web applications if you want to publish a website you typically build with asp.net core there's ways to do that using some other things with net but out of the box we use asp.net core as our framework um windows desktop app if you want to build windows desktop applications you would use the windows desktop app framework to build appropriately with there's two different user interface frameworks there that you can use one is called windows forms and that's the the typical battleship gray you know forms that you would click through and interact with and there's also wpf which is a more um graphic native graphic card friendly development environment that allows you to be a lot more flexible with how you build and write your applications it uses a a version of a markup framework not html like we have for the web but a markup framework called xaml xaml that we use in wpf to build those application user interfaces we also use xaml with the xamarin mobile framework that we use with dotnet with c-sharp so that we can structure and build user interfaces that'll work on iphone and android devices so lots of different options that that are available to you with net to build applications um i'm going to take a look here at the chat room [Music] okay so um now that's great you know i'm seeing you know these are the different frameworks that that came out of the box or are already installed on my machine because i i've been building and working with netcore with net for a very long time um i guess that makes me old yeah that's the thing um but when we want to start building an application we have templates that ship as part of this that adhere and provide and work with these frameworks so that you can get started building an application so i'm going to create a new folder here so that i can i can work and i'm not using the exact the exact project files that were created as part of the uh the source here and i'll go through and write this code along the way with you live on stream so um let's call this uh uh learn about um learn about projects okay so i'll go into that folder and i can run dot net new and it will show me a huge list of all the different project templates that come out of the box with the.net sdk right the net command line tool here we call it the.net sdk the software development kit sdk now it's just a command line tool everything kind of jumps out of this.net tool for us to interact with and do different things with our code whether it's generate some base template files so that we can start building a project connecting those projects together with other projects getting package references like like you may have done with a um with a node project right you have a package json file and you you do an npm i to install package references from the npm the node package manager resource well we can do that as well here in dotnet so we will also build our applications we'll test our applications all using the.net command line and there's tons of great templates here there's more templates that are available that have been built by the community that are available for you to download and install or maybe you want a specific template that folks at your organization at your place of work should it here too that have some standards some some standard references maybe there's some some logo some various elements that are um that are required that you want to have in all of the projects that your organization has you can build your own template and install it so that folks always have a common starting point to build their applications always possible to do that and all of these tools are extensible they're open source you can find them on github if you go to github.com d-o-t-n-e-t and you'll find the entire repository of all of the tools all of the libraries that the net team is building let me take a look here at some of the comments in the chat room and and catch up here um there's a couple of good questions i want to make sure um [Music] let me answer this first question from uh palaman will dotnet core eventually eventually replace dotnet framework or dot net standard i've purposely avoided talking about net standard um as much as i could here on these streams um but there are there are three four dot nets that are that are out there dot net core dotnet framework dot net standard and dot net xamarin dot net xamarin is that user application framework and tool set used to build for iphone and android um and a number of other targets but primarily you'll see folks use it for iphone and android um net framework is a framework that was originally shipped in 2001 that helps you build websites console applications class libraries desktop applications for windows it's ships as part of the windows operating system you will as long as there is a version of windows because so much of windows and the the tools and features inside the windows operating system rely on.net framework for how they're struct for how they're maintained um you will always see net framework being shipped with windows it's never going away net core was a new version of net that was built and released um they started building it in 2014 was first released i believe it was 2016 and is intended to be a smaller lighter and faster version of net completely rewritten has a lot of the same features as net framework but was intended to be built and used for the cloud originally was intended to be built and used for the cloud it's evolved it's brought in some of the features from.net framework you saw already windows and i think it's here somewhere windows forms applications uh wpf applications as well and has asp.net in there so you can build projects appropriately for for the web for the cloud and it also has the cool new blazer framework that you can use that you can build for grpc you can build apis all kinds of great stuff that you can build with net core the next version of net core is what we're calling.net 5 and it brings in the last of the features from.net framework that are intended to be used going forward as the light and fast framework that you can install quickly and easily not just on the cloud but also on windows linux and mac environments dot net core supports windows mac and linux amazingly very very well top-line support you have for all three of those there isn't one operating system that's treated better than another in fact net core was specifically shipped outside of windows so that we don't have to wait for um we don't have to wait for for update tuesday in order to update the framework if you need to update and we need to ship a patch for it we can ship those patches immediately you can apply them and you'll have great support for those frameworks so there is great love for all the operating systems from net core and the next evolution of it that's being referred to as just net five the next question that we have here in the chat room is from anti-dotos auntie dottos let me anti-anti-dot os i'm not sure do i feel like a lin uh uh in a linux platform when using command with.net core as a microsoft tech developer no i don't i feel like i'm on the command line i'm used to being on the command line because um as a as a more mature developer i've been around and and writing code for windows and dos operating systems before that since the 90s so working in a command prompt a command shell um is something that i've i've always been accustomed to um i very much appreciate that the command line is the one unifying point that all operating systems have whether it's windows mac or linux you have a shell that you can drop into use that command line to build and work with whatever it is that you might want so the commands that i'm going to be executing here like dot net new work just fine whatever your operating system is so as a microsoft tech developer i'm okay using any of those three operating systems on my stream here on twitch twitch tv slash c-sharp fritz we spent the last month working exclusively in ubuntu in march on that same channel we spent a few weeks working exclusively on a chromebook inside of chrome os with.net all the tools to make you productive and they work everywhere really really great stuff no lithics i am not a senior developer i've aged like a fine wine i'm i'm mature i've i've gained wisdom knowledge and i'm passing that on to you um jeremy knight has a question here and then i'll get back into our project does net5 have an answer for cross-platform graphical user interface wpf windows forms anything like that it does not um this is where the version a a uh part of the dot net five effort that was postponed to net six um [Music] it was dropped um it gosh year 2020 um software developers haven't postponed or dropped any features and delayed any things that they've been trying to accomplish um yeah i'm looking at you uh cdpr and cyberpunk 2077 um delayed again um they delayed this feature the.net team delayed this feature that was initially called.net maui that was intended to be a multi-platform user interface library that would work on windows mac uh and mobile environments out of the box and provide support so that linux community members could help inter integrate their favorite ui stack with maui and be able with net maui and be able to make that uh work on your favorite uh linux distribution um but the the scope the size of that effort and and pandemic caused a decision to be made to delay it for net six so no they did not delay.net maui to clash with 20 cyberpunk 2077. no no no um [Music] that's a fine question mufana blue i don't mind answering this and i'll get back into the code so what's the point of installing and using net5 since it's not released as an lts version if dot net 6 is the next lts would it be better to wait for net six this is a good question and and this brings up in this support cycle there are two different and you see this in in other open source uh frameworks there's the difference between the current version and the lts version the long term support version dot net core 3.1 is an lts a long term support version that you will have three years plus one year to convert to migrate to the next version you'll have three years support a current version like net5 and net core 30 before that were current versions that you um had three months to to upgrade to the next current version that is released now why would you choose to install and use a current version instead of waiting for an lts it's it's really a question of trade-off long-term support versus improved performance tools and techniques that you may need access to if if you want to build with the new latest and greatest there's a grpc um api in here that is the fastest running grpc server and client that you can get anywhere on the web uh and you can learn more about that on the uh dotnet blog but if you need that performance you may want to upgrade and build with net5 if you need support well okay stay on net core 3.1 there's nothing wrong with that it continues to be supported and wait4.net 6. this is about choices this is about flexibility there's folks who do like to be on the latest cutting edge versions of things as they prototype and build because when we go from net 5 to net 6 there won't be as much of a change as going from net core 3.1 to net six so you train all trade off migration needs and techniques with advanced tooling support and these are choices that that we all have to make certainly if you're building a system that you need to support for the next 10 years absolutely stay with net core in that long term servicing release wait for the next one and and since we know that net releases going forward will be released in november november of each year there is a net version that should be released since we know november 2021 is when dotnet 6 is going to be released you could you could plan okay well i'll do my team's update from dotnetcore312.net6 for january 2022 take a month to experiment and make sure that evaluate that this is exactly what i want to do and do that patch update in december and i'm sorry in january 2022 you have that flexibility and now you have that knowledge that you can use to schedule appropriately so all right and um yeah it dot net six that should lithics points out.net six that should come out about the same time as cyberpunk 2077. no no no we kid for those of you who don't follow the the reference here cyberpunk 2077 is a game that was originally announced in 2013. um it it won game of the year for i forget which show it was in 2013 2014. the marketing folks were working hard on on this game um previews were were and playable demos were released in 2018 it won game of the year at e3 in 2018. they were revised and a release schedule was published it was supposed to come out in 2019 it won game of the year at e3 in 2019 they had to delay it into 2020 because they weren't quite ready for it it won game of the year again at e3 in 2020. it won game of the year after it was delayed because pandemic to later in the summer it won game of the year in uh from twitch for 2020. it was delayed again to the fall because they weren't quite ready yet and now you can buy all the custom xbox and controllers and funko all the neat all the cool stuff that comes with the game that you would expect um and it was delayed again to december 2020 because they're not quite ready yet we'll see more more coming soon so it's um it's interesting it's won game of the year three years running and still hasn't been released so um this is a problem that many of us as software developers have and around managing scope managing expectations communicating with our marketing team so that they let our customers know when new versions and new features are going to be released so you make choices like the dot-net team did to cut net maui from net 5 move it forward to net six because we have a scheduled release date scheduled time frame when that's going to come out all right let's get back into our project these are all the templates that we have let's start building let's start building a class library right that's the typical uh no you know what here in the demo i think i actually talk about building a console application so we can build and actually run an application to start and we can build our first application by saying dot net new we specify the name of our template and i use the short name here so that you can reference and build that i'm going to build a console application that runs right here on the command line i can choose which language to write things in by default you see in brackets c-sharp is the language that we use to build these so i'm going to build a new console application and i'm going to pass in the o switch here to tell it well write it out to a folder and i'll call it app one now this command is actually going to run two different tasks not only is it going to create a copy from the template of the console application but it also is going to restore any references there's there's packages that are referenced um inside of that template that we need to copy and make available on our machine now if you've been running projects if you've been building projects for a while on your machine it won't have to download them it keeps a copy in a central location and all that um all that that restore process does is just creates a pointer to where the version of that package is on your machine so you don't have to waste drive space nobody likes when when frameworks and tools waste space on on your hard drive so there we go all right so it created and in 97 milliseconds it restored and i can now um go into that app folder i can take a look and i have i have an obj folder that's where the information about where those packages were were found is written into the obs folder there for us as humans you don't have to go into the obs folder 99 of the time there are times you're going to want to peek in there you can go ahead and take a look in there but it's not a folder that we should maintain by hand but the tools maintain that that's intended to be a machine maintained folder but there's two other files here app one cspraj and program cs if i take a look at program cs you see c sharp code we're used to seeing c sharp code here it is our first program and it has a static void main method right we've seen methods before and it says console writeline hello world easy i can run this application now with the command dot net run now dot net run actually does three things for us first it outputs hello world that's um first it will look to make sure that the packages are restored properly and right there's there's stamps and hash codes that are passed around so it it determines very quickly that it doesn't need to restore second it compiles the application right and compiles something that we haven't discussed yet right for those of you that aren't used to using a compiler real easy to understand the c-sharp compiler that is triggered by the dot net command line here we'll wake up take a look at this text that we wrote the c sharp text and turn that into executable content that we can work with finally it will actually run that executable content and we get the console writeline it wrote out hello world easy real easy to understand those few steps that it did now where did it write that out if i look at my folder now you see i have a bin folder that i can go down into if i go down into bin you see there's a debug right so if i go into there there's now a folder called net5 this is the net 5 version of the project and if i take a look there there's my executable app1.exe a bunch of other files here that have information about how the project was structured what it was how we can go in and debug it if we have a a tool that has a debugger like visual studio or visual studio code you can use a debugger and these files will be used by that tool to allow you to interact with your code and step through things set breakpoints so you can work with it so not bad okay i can understand this and i can actually i should be able to run app one directly here no you're right now i'm going to there we go um i'm getting i'm running in powershell here so it's confusing me but there we go hello world and it outputs quickly no big deal nothing groundbreaking here but what i do want to show you let me go back up um there was this project file also right this cs proj now if you're used to python you don't really have a project file or maybe you're used to um typescript or javascript working with node and you have your package json file that has references to all the packages um has some information about how you can be turned into an your project can be turned into a a node package well how do you write you you have that manifest file that defines those things so this is something that we can dig into and take a look at and you'll see it's got a very simple format here it's xml so that it maintains compatibility with all of the.net projects going back all the way to 2001 back when we believed xml was a good format for storing and presenting data you'll see there's an sdk here this sdk attribute on the project says we're using the.net sdk the generic.net sdk there's more specific ones that we can use for different frameworks and you'll see that a little bit later um we specify a property group that says well we're going to output an executable file and we're outputting that for the target framework.net 5. now there's other things that will appear in these project files as we add references to other packages to other projects but and we can also include information in here about how we handle the various files inside of our project by default files that end with a dot cs extension those are those are c sharp files those files will be compiled and included in in the binary output of the compilation process let me take a look over here at the chat room and see if there's any um any questions that i want to take a look at here catch up with um um hey shane good to see you um priyanka has a question here that's a good point i i kind of skipped over that let me let me touch on that question that you have there very good point um the prank has a question here is there a command to only compile and not run like the java compiler does absolutely you're right i kind of jumped through that quickly um i can execute i mentioned when i when i executed the.net run command it restored packages built and then ran the executable at that point you can run those separately they're separate commands i can execute.net restore to restore packages and references i can run to specifically to compile and not build i can run the dotnet build command and it will build and tell me if there's any warnings or errors at that time it'll also tell me i'm using a preview version of net so that i know it's a preview version i'm going to need to update at some point finally i can dotnet run and it will go through do those three steps and run my application you can pass some switches into these if you pass dash dash help it'll show you all the configuration options for whatever verb you want to execute with net right if i just execute dot net it'll show you the basic switches here and you're hiding behind priyank's question um right but dot net help and you can see here's here's the base verbs that the commands that you can execute from net there's other commands that will be added as you install additional tools because just like our templates the net command line is extensible you can add features to it so um if i take a look at net run because i've already built because i've already restored i can actually say net run no build um and it will run it immediately and not even attempt to restore packages or build the project it just runs the last version that it has built that might be important to you that might be something you want to do in your continuous integration processes so at the command line that's how you would do this i see some folks in chat talking about working within the various development tools development environments whether it's uh i see visual studio 2019 i see some folks talking about visual studio code um very cool yeah i'm not going to get into those specific tools i want to stay as simple as possible here and and we'll get into using visual studio 2019 in the near future so all right um so i have app one i built my first application there's all kinds of options and tools that you can you can put into that configuration file that cs proj file there's a link in the documentation um in the readme for today's session if you want to learn more about all the configuration options in the project file for the most part what i want you to understand is you don't need to touch that that project file by hand you can pretty much work with that using just the dotnet command line tool so let's start building our first project with some code that we can actually start testing we'll call this project logic that'll be part of a simple shopping cart application and we'll we'll build up to this uh very quickly so um let's make it let's make a new folder here and let's call this uh let's let's call this a shopping cart okay um there we go jeff can't type so let's create a our first class library and let's just put our business logic in this this is our shared business logic it's a it's a class library it's going to restore place all the content inside of a a folder here called logic and it by default has this first class class one cs right and a class library is a reusable unit it's a non-executable unit you can't build a a standalone application from a class library this is a unit of binary code that you're able to reference from various projects various user interface frameworks whether it's a web application a mobile application desktop application uh application that runs on web assembly you can use class libraries across any of those different frameworks um let's take a look at what that project file looks like now remember earlier i already cleared the screen you can't see it remember we had a right we had an exe entry that was dropped in here that told us that the output type was an executable well we're not outputting an executable here so that output type exe isn't present we're still outputting for net5 and we're using the.net sdk to build this this bundle of objects and things that we're going to reference going forward so um i'm going to use a visual studio code to work inside this folder i'm just going to copy that and open visual studio code here a lot of folks like visual studio code it's um it's currently the most popular um text editor developer tool on the planet so that's pretty cool we can use that so i'll open folder here and we'll start working on our shopping cart so i have my logic folder and i'll open that class file and here is the the basics of that class file and i love that they use space indenting here um for those of you who may not have seen um not may not have seen some of my other streams my other videos um i'm not a fan of space indenting it's it's not accessible to folks who have vision problems so it's it's something that um is important to me that we encourage folks who who might not be able to see your code to be able to read and understand that appropriately so um let's define what a product is that we're going to work with here i'm going to control dot and um oh you're not going to let me do this it's not there that's fine over here product and i should have let me make sure i'm going to restart karnak i have a an indicator that will run up here in the corner and show you exactly what hotkeys i'm running there it is ctrl s to save so as i'm using visual studio code you can see the hotkeys and uh maybe learn how to better use visual studio code from seeing some of the things that i use um see that look at that it's using spaces anyway you make me sad um so i'm going to define yep you're you're going to be a pain in the neck here aren't you um and not give me there we go there's snippets that you can use with visual studio code when you're building now i'm getting tabs when you're building properties you can type prop and it will give you an automatic automatically implemented property here so i'm going to implement a product id um come on now there it is uh we want a name right um and let's provide a decimal price for our product simple it's a product there we go and it just works i'm going to open a terminal here inside of visual studio code i'm going to drop into that logic folder and um let's actually build our project right i can work right here inside the integrated terminal and dot net build it's not much i just put a class in here and it builds successfully no warnings no errors and away we go good that works great um jeremy has a question here in the chat room regarding.net 5 what's the story for sharing code to net 3.x projects older versions of net is it just using.net standard yes you can use net standard 2.0 and 2.1 to build projects that are capable of being referenced by older versions of net whether it's net framework xamarin or dotnet core going forward we're gonna have everything in one.net there will be just.net5just.net six as xamarin gets folded in and becomes net maui and we have one user interface framework that we can use to target all the different devices different operating systems so for if you want to target the older projects yes use net standard so and i'm like i said i'm purposely trying to minimize some of those complications by focusing on.net 5 today so great i've i've defined a product i can build it and now i have this object big deal right we also have something here that i need to call out that we haven't seen in our notebooks we have these things called namespaces and the namespace is just a logical unit of organization for your code that you can use to to uh navigate and reference and get around the various projects um [Music] and the various binaries that your project may be referencing so that's important to be able to have that level of organization now in net projects [Music] i don't know what that was in net projects we typically create a root level namespace that has the same name as the project logic um if i were to create a child folder that had the various accounting business logic in it right if i were to make the accounting folder here and go into that folder and right so there's my folder here if i were to create a new file in there and write whatever some file by default um the namespace that we would expect to have in here would be uh logic.accounting this way we have when we're writing our project when we're writing our code we have the same logical groupings on disk that we do in our code right you effectively by looking at the explorer pane over here on the left you effectively have the same organization for your classes interfaces enums the various objects that make up your project you effectively have the same organization for those as you do inside of the dot net memory space i hope that makes a little bit of sense how's it going chris good to see you so um but i'm not going to use tests yes i'm not going to put anything in here but this is what it would look like and i would then be able to reference objects from inside of that test cs right let's create a class here um public class test i'm not going to put anything into it but if i created a method here um public void uh not food foo um var uh t equals new test well wait a sec it right i can't find test it's not in my list of things that it's suggesting and if i force that right i get a little red squiggly here because it doesn't know what that is and can't find it i can't find it because it's only looking in the logic namespace it's not looking down into the accounting namespace right it's and that red squiggly line that red underline right um is being being delivered to us because there's a compiler behind the scenes are you kidding i didn't save the file there's a compiler behind the scenes that's inspecting and compiling our code and it gives us an error and yes it's it's red text makes it easier for those of us who who can see colors to be able to see that hey there's an error here um and it's it's clearly saying the typer namespace test could not be found well how do we include a reference to that accounting space so we can we can reference test well i mean i could say new [Music] accounting dot test and it'll find it right and i can go as far as to provide the entire this is called the fully qualified name of that type logic.accounting.test i could do that if i want to be able to say just new test and reference it like it was here right next to product i can create a using statement up at the top here and you see one using system right we never had to do this in notebooks because it automatically resolved these things for us but we can say using logic dot accounting and it will now be able to find that test object and build properly so that using statement is the same as what you might find in an import statement in other programming languages where it's saying here's a hint of another place that you can look in the object hierarchy that's currently referenced in order to go and find objects that are that are referenced inside this file only yes that is a lot better than when you were using c back in the day and the only message you got was segfault and no idea where it is right right um if i comment this out um the other bit of that error that i want to make sure that your you can see here is it's clearly telling you on line 17 actually position 17 on line 17 there's that error let me bump that down a little bit so that you see that it tracks appropriately row line 19 so if i scroll down line 19 position 17 is the test reference there and inside visual studio code i can click that and it'll jump right to i hold down control click that and it jumps right to where it is and i can read the error and address it appropriately now there's all kinds of little tools inside visual studio code you'll see this inside the other developer tools as well where i can hold down control and hit dot and it will generate using statements for me if i roll that back control dot i can actually also have it provide the fully qualified name if i want to go that way your choice coding is a little bit of an art choose what makes you happy to build and write this code so great i've i've got references to these things and i mean i don't really need this for what we're going to be doing today um i do need to end a little bit early so i need to get on the move here so that we can start interacting now this is one project logic but if i'm going to group together a series of projects to build an application to build a larger system i want to make sure that i put those in a unit and we call that unit uniting.net the solution now solution is represented by a solution file that is written in a very old syntax that was used by much older microsoft built integrated development tools um that you really once again shouldn't be maintaining by hand we have tools as part of the net sdk to help you generate that solution file and manage the content of it if you're in a full ide integrated development environment tool like visual studio 2019 or visual studio for mac there's um visual menus that you can use to do that management to do it at the command line the simple way that that you can do this which is actually the same commands that those tools execute and interact with um is using the dot net new command again to build and interact with that solution file and the solution file is typically at the root of the solution and you have child projects that live in the folders underneath so i only have the logic project folder here i will add a solution file an sln file dot net i'm sorry dot net new sln and it will generate a solution file here for me you see it appear over here we can take a look at it and it's it's not a format that you're going to recognize it's like tab delimited with vertical pipes and properties with equals and there's sections here that are tab indicated it's not entirely clear what this is doing but i need to add projects into my solution so net sln add add a project to the solution and i'll add the project that's in the logic folder and you see my solution file changed i've now got look at this now i've got all these other things down here in these guides right these globally uh unique identifiers that are being referenced here and there's a definition of what that project is and it's it's got a pair of guides that it's passing around i don't want to have to memorize and reference and figure out what these things mean back and forth that's a pain in the neck nobody wants to have to deal with that use the solution command line tools to interact with those not bad okay dot net sln add logic let me add a couple more files into my into my logic project here i'm going to grab them from the source code that i've already written i'm just going to grab these three files copy and i should be able to just paste right in here here we go so just so you don't have to watch me typing you know what i mean nobody likes to watch for its type um so i've added an order object so an order has an order id it has a name of the customer we're going to ship to it has a collection of order items a coupon code some coupon savings that were realized because of that coupon code and a total cost of the order now those order items relate to an order id they have themselves an item id um and they refer to a product you put products into an order and those products are line items you have a number of products a quantity of products that you're referencing on each line item and a total price for that finally i've also added a coupon evaluator that has our business logic apply coupon to an order and if the coupon code on that order is save 10 well we're gonna we're gonna generate an entry in your coupon savings that says that the total cost times ten percent ten percent of your total cost of your order are your coupon savings that's now that's a deal that's a deal come shop at fritz r us and get your 10 off for the save 10 coupon code um so very simple set of objects here that we're working with um let me get rid of this i don't need this go away goodbye all right right good uh product id name and price good so um i should be able to still build and i can build all the projects in my solution by running.net build in the same folder as the solution file it'll go find all the projects and build them for me and even though i've added these three new files no warnings no errors everything builds properly great but i think when you look at some of the things that we've dropped in here we don't have all the logic yet we we still need to define some of this and i'm not quite sure how we're going to manage coupon interactions going forward that's something that i want to make sure doesn't break now this is where we start to want to introduce tests and automated tests [Music] for for many many years and and still there are a lot of folks that work in the quality assurance department they do quality control they review and make sure that applications work the way they're supposed to but we as developers can also write tests that show we can write automated tests write code that exercises our code and those tests can be executed to verify nothing broke from the way that the test intended for it to for that code to uh be executed and evaluated so you'll you'll you'll have various levels of tests whether it's a full system test that somebody who works in in quality control quality assurance runs and makes sure that everything in your system works properly front to back that somebody clicks on the button and it does a thing that makes something else happen and they get an appropriate record that's delivered to them or a shipping email that's sent to them that says oh they've properly ordered and this is the appropriate contents that we're going to ship to them that's a full system test there's integration tests where we're going to make sure that when you click a button on a user interface it executes the business logic appropriately and an inappropriate entry is delivered back to that user interface that's an integration test but the smallest test that the narrowest scope of test that we can help with as developers is called a unit test and i grabbed a reference from wikipedia um let me open this up here yes i know i clicked on that so that you would open it there's a definition for unit testing here in wikipedia let me grab this for you and drop that link into into chat um unit test is a software testing method by which individual units of source code sets of one or more program modules together with associated control data usage procedures and operating procedures are tested to determine whether they are fit for use plain and simple we're going to execute a small individual unit of source code in many cases a method inside of our classes our objects and make sure that that behaves the way we expect it to we'll give it different inputs we'll put that method into different scenarios and make sure that it behaves the way we want it to that's easy that makes sense [Music] looking here at chat catching up okay yes lithics very good point there let me bring up lithic's point here um because the this is the value in unit tests when they're compiled they're saved and shared with your source code and will show that your application that your source code is still doing what you intended it to do a long time after it's written years and years down the line so a unit test i tell folks is an investment it's a little time investment that you make now that years down the line you might not be with the project team anymore but skippy or whoever the developer is who comes along and is handling that project after you they can run your tests and they know that the code that they're now maintaining still behaves the way that you intended it to run years and years ago or whenever i don't do time anyways so it's a it's a fantastic way to folks will also refer to it as pinning functionality right there there are some folks who are against the concept of unit tests because i'm writing code to test my code of course my code works i wrote that code don't you trust me as a professional developer it's not that i don't trust you it's that i want to notify and provide a safety net an insurance policy for the person who picks up and manages that code after you i want that code to still behave the same way that you intended it to run and by writing just a couple of lines of code to help assure that you're now providing that guarantee and when we look at modern development processes devops processes whether it's using a tool like jenkins or azure devops or github actions that will run your unit tests every time there's a change in your code now now we have the assurance that even before it reaches a customer your tests pass and there are test suites out there that have hundreds of thousands of tests and they take minutes hours to run but if you have such a complex system and you can find out in in minutes that you didn't break anything in in hours that you have such a complex system and you didn't break the way that something was intended to be run that's a tremendous investment that's a tremendous time savings that you don't have folks clicking and walking through processes to make sure that your code still works so unit testing is is a process is a development technique that has become more and more appreciated and approved as a standard process in many organizations and certainly in many open source projects where you're going to have folks contribute a little bit of code and walk away from the project well i want a unit test to go with that contribution because you're not sticking around you're not helping with the project going forward and i want to make sure that it continues to behave the same way so that's a little bit about unit testing and unit testing is a is right is a practice that can be applied to any software language or framework and herein.net we have several different tools several different frameworks that we can use to facilitate building tests and we actually saw them pop up in in our.net net templates so if i run dot net new again you'll see these unit test frameworks pop up here they are where is it i don't see yeah there it is they are right here we have a unit test project using ms tasks ms test is a test framework that was built by microsoft um in the early 2000s that was intended to be used with visual studio so that you had a complete test experience n-unit is a test framework very simple test framework that was built as a migration from the original java based unit test framework called j unit and x unit is the evolution from end unit version 2 to a more feature-filled test framework that was intended and designed to adhere to the way that net developers see sharp developers write and want to interact with their tests so um you have your choice there's many many other unit test frameworks these are the primary ones that are they come out of the box with the.net sdk these are the primary ones that you'll see folks use they all behave similarly they all all have very similar features in defining what tests are and the various test methods that you can interact with inside of your classes so you can choose to use one of these we're going to choose to use x unit priyank i see your question there let me pin that and we'll we'll talk about that in just a second um but let me add a let me add a test project and add it to our solution here so um i will create a new uh x unit project that's not how you spell x unit x unit and i'm gonna write that out into the test folder here so it's going to create it restore and place that file appropriately i'm going to add that to my solution file so that we have that available [Music] and i'm going to go down into the test folder let's take a look at what that test project file looks like there's a lot more content here now in fact let me open that over in in visual studio code because it'll be a little bit easier to see with the syntax highlighting here once once again.net sdk it's not producing an executable target framework is.net 5 is packable can we turn this into a a package for reference no this is a test project this is intended to be used with another um with another library right this is a developer thing and it adds these package references so the microsoft.net test sdk how can we test execute tests from this project there's x unit there's an x unit runner if we're running inside a visual studio that will be referenced and there's information here about the various assets and things that will be referenced and used from that package the the last package here is called coverlet collector these are the names of the nuget packages that are being referenced coverlet collector will help you collect and generate what are called code coverage metrics how much of my code is actually being executed when my tests execute coverlet will help you review and identify those metrics i'm not going to get into code coverage today there's great documentation about that in the dotnet docs and i there are links to those to those documentation articles inside the readme for today [Music] um the question from priyank here i want to bring up how do you convince clients how do you convince management folks that it's necessary and get budget allocated for unit tests this is a problem that i ran into all throughout my career um folks don't appreciate what you're gonna write more code you're gonna write twice as much code so that i have tests that tell me that your code works why doesn't it just work the first time or are you not confident in your code this is a question about like i said creating that insurance policy creating that capability to show that you're going to in the long run save money because adding this little bit of investment of time now it's not going to take you twice as long to write the code it should take you about a quarter to a tenth of the time to test the code as it does to write the code um but it's going to help give that investment so that in the future you're able to identify when things break immediately and it because let's face it the cost of a bug getting into a customer's hands is significantly higher than the cost of adding a little unit test now that assures that it works properly forever into the future that cost of of a bug a bug being identified the reputation cost now that your customer found a bug the cost of your quality assurance and operations team to identify and give you the log where that bug occurred the cost then of your operations team to or operation support engineers to go investigate and fix that bug the cost of redeploying that fix and the cost of now reclaiming that reputation that you lost because your customer found a bug that's significant compared to the extra two minutes five minutes that it's going to take you to write a unit test that's a pretty good trade-off so um nick's provided uh information and sold unit testing to nick's manager as a means of providing to the client a number to help measure quality sure the number of unit tests definitely helps indicate a quality level however the number of unit tests is relative to itself in time by that i mean you might have 10 unit tests when you first have your minimum viable product as you add more features as you finish shaping the features to your minimum viable product that number of tests may go to 15 20 30. does that mean it's three times more three times better quality because it's now 30 tests instead of 10 no does it mean you have three times as much functionality with the same amount of code coverage no it means that you've got three times as much code that's being tested and three times as much assurance that's being delivered but if you're going from zero assurance that's a lot if you're going from ten percent assurance okay that's an improvement but it's relative only to itself you can't compare those 30 tests in your project to the hundreds of tests in somebody else's project that doesn't mean that you're you've got so much less quality than the other project it's only relative to itself so all right let's move on i've i don't have too much time left here i need to get through and actually show you how to write a test but if i want to write a test right i'm going to end up writing classes that look just like other classes that we've written in the past and we add these attributes onto our methods in this case an attribute like fact now this tells the x unit test runner that this method is a fact that should be executed to ensure that it ran properly we don't want to see any errors when test 1 is executed so i can drop down into my test folder and i can run this test not by net run dotnet run doesn't run the test it'll build but it there's nothing to run so it doesn't output anything to run a test project's tests you run another verb test so dot net test in a test project does the same things that dot net run did restores packages builds and then for that test framework calls into the test runner points to the dll the class library that we built there it is test.dll points to that and says hey testrunner run any tests you find inside of here now there's other attributes you can decorate your methods with besides fact i'm going to focus on fact today and when it finds those methods it's going to execute those methods and make sure no errors are raised and it gives a little report here passed no tests failed [Music] one test passed no tests were skipped and a total of one test method there was test one was evaluated it took two milliseconds to run that one test now when you get into a bigger project and you can tell you can tell your boss we knew in seconds that our project still works the same way even though we added new features x y and z that's fantastic you know you didn't break those older features right getting this little report is amazing this is what you want and when we're going through and writing these tests right i mean we're not doing anything in this method we need to actually start doing something with this so let's write into here something to test so there's a method called there's an object called assert that comes with x unit that we can use to test if things are equal greater than not null should be null i'm going to assert and let's do something really easy here let's assert that two values are equal let's expect the value two and test the value three right or right maybe i've got some value that i've i retrieved from some other um some other method right and let's call it the actual value and let's say that's three and i can specify here actual value so what assert does is it says take this expected value to inspect the actual value being passed in from the actual value variable and see if those are equal if not raise an error i'll run.net test and um [Music] you make me sad fine assert equal um do that one more time uh what what what you're using a preview version yes i know why didn't it why am i not assert assert equals is obsolete this is an override use assert dot equal i did i didn't save the file net test there we go so one total file and there we go assert equal failure um expected the value two but the value that was delivered was three and it shows me exactly what line that was where the test failed and i get my little report here at the end one test failed zero passed zero skipped one and it took five milliseconds right um and you can have many many uh test methods here um do the thing right and um var x equals one and i can write say assert uh search equal one is x and if i run that again right it'll come back and tell me one failed test one failed and the other test did pass there were a total of two tests and it ran in 28 milliseconds so now now we're seeing our tests right we start to add to them and of course i can fix this well actual value i i would go and fix whatever business logic was delivering that and if i run that test again it should come back and we should get all green there we go all green seven milliseconds everything passed now i i know there is a little bit of an accessibility issue here red and green right for folks who might be colorblind it's you're not going to see a difference in those but of course it has the text at the beginning failed and passed to indicate that the test report failed it failed if any number of tests failed it passed if there are zero failed tests that makes sense and you see right we've only got two tests here it it's right very very quick that it's executing these and we immediately get that response yes so some folks are getting into um some of the other concepts around unit tests so what if what if i wanted to run these tests whenever there's a change right whenever there's a change to these files go and rerun the tests well there's a command that comes with the net command line called watch and i can pass another verb after that that i want it to rerun should any of the files in this project change or any of the projects that this project references change go and redo so i'm going to execute.net watch test you see it starts watching it's going to go and execute all my tests and now it's waiting for a file to change before it goes and reruns the tests again so if i went and changed some of my code in whatever other project and the actual value now is three and i saved that file i just saved the file i didn't re-execute anything down here it automatically went re-ran my tests and in seven milliseconds i haven't even left this file yet i'm still working and it's automatically telling me you broke the tests so hopefully getting that immediate feedback for us as developers will help to get you to go back oh shoot i went and changed that thing in that place and hopefully it fixes that back to whatever value and now i know my tests work again there are plugins you can get for all the various tools out there visual studio code has a bunch of plugins visual studio 2019 has plugins that you can use that will automatically run tests for you as you change files as you build projects visual studio for mac has them as well there's all kinds of great tools out there for this i have a little plug-in here this is the dot-net test explorer and i can tell this exactly where to go to not to find um find my project so test project path and go find it in the test file this will go and discover tests and it will come back and show there it is there's my tests and i can click this and it will run it and i'll know immediately and i'll see here in the panel that my tests work so if i'm over here working right i don't need to have this watch running here in the background right i can just be doing whatever thing over here make my change and i can execute the commands to run this alt r alt a right well let's make sure that my tests run and you can see they're all executing and i know immediately oh i broke test one and the plug-in automatically highlights well there's there's the line that through the arrow and it shows me with a little peak window there exactly what the error message was and i get a little um indicator that shows which tests passed and which ones did not and i'll go rerun the tests and i'll see a couple questions here let me let me bring these up there's all kinds of tools that will help you with this um but vival vivalla manuel asks will it only rerun tests that have to do with the files you changed um if it's in a project that you referenced it'll rerun everything if it's in a test that you changed um it it actually when you do.net watch it reruns everything you can use smarter tools that are available that will appropriately find exactly those lines of code and things that are referenced that are impact that will only test those things that changed there's things like live unit testing inside of visual studio i see somebody else here um john callaway mentions endcrunch is another tool that do's that do that does this level of analysis and will give you exactly those lines of code that are broken and and only execute those um tests that are impacted and and give you a much quicker experience at evaluating the the impact of your tests the impact of your code changes some people like that some people don't but there's all kinds of uh tools out there that you can use that will help with that and i'm just showing you a couple of simple ones i'm here in visual studio code.net test explorer and netwatch test you can run that if i i like to have tmox running on linux and i'll have inside of a lower panel my netwatch test running that shows me whether i broke my tests and in my top panel i'll have vim running and i'll be writing my code up there making changes and when i save files from vim i'll immediately see that my tests ran and executed properly the test extension that i'm running over here is dot net test explorer um right test explorer you're not gonna show it to me are you um yeah net core test explorer this is the one that i'm using so easy to use um [Music] and helped out right there um yes our friend scott hanselman has a blog post about um generating markup here in visual studio code that'll give you information about what lines were actually executed tests that passed and failed inside your code i am just about out of time and i'm i'm way back here i haven't gotten through most of the code that i wanted to say we had a lot of good conversations um i do want to show you how to add references to other projects so we're inside of our test project now let me go over to the console we're inside of my test project and i want to test code that's over in the logic project well you do that with the dotnet add command and the dotnet add command will let you add references to new get packages those are available at newget.org or you can add references to other projects that are on disk so i'm going to add a reference to the logic project from the test project so now if i take a look at that test project right we had these package references here to all of my test framework things but now it adds an item group and there's my project reference so that it knows to use the logic project as part of the test project this means if i go back over to visual studio code um inside my unit test one here right um let me run dot net watch test here if i add let's add another fact public void um and let's say um right we're going to take a look at the coupon evaluator should uh should give um it's a uh what was it called safe 10 when save 10 uh code is used um should uh grant 10 savings notice the name of my test here the thing that i'm testing coupon evaluator i have a when here when the save 10 code is used should grant 10 savings i'm very clearly defining the the narrow scope of the coupon evaluator that i'm going to be working with and i like to typically use a 3a pattern here um arrange act and assert now i save that and it's going to tell me that test passed right it's a three test passed but i haven't actually written any code in here yet um so a range i'm going to set up the things i need to test that uh that 10 code so let's create a new order um and let's put in that order uh well the coupon code right um it is coupon code there it is right save 10 um and that runs off of the total cost right so i'll say total cost equals let's make it uh really simple um equals 100 right um act so let me create my my coupon evaluator um and i'll just call that a coupon evaluator new coupon evaluator good and i want to evaluate i want to actually run it against my uh my order here um let me reload the window so that things get set up properly here for me thank you down here okay and this is going to come back and tell me hopefully very quickly um oh it doesn't know what coupon evaluator is right because that's in a different namespace right so there we go i got a red squiggle i'm going to control dot i need to bring in my using statement for the logic project so i save that it's going to go and restore it updated and built both projects i'm still passing i haven't written any code to actually evaluate anything but let's act let's actually act on applying our coupon right that's that's this method here i'm gonna apply the coupon to our order and i should get um a coupon savings that is ten 10 of that so i'm going to pass in that order and i know that 10 of 100 is 10 right so i will assert dot equal um i'm expecting it to be the value 10 and uh it should be right coupon savings is a decimal um i'm going to take a look then at the coupon savings and see if that matches so it's going to run that and i should know immediately if my coupon evaluator works it does i can make sure that right that it's calculating it correctly well is it equal if i say it's one dollar that it was saving no it breaks and i immediately know um i expected it to be one dollar but it actually delivered ten dollars us dollars but i'm in the states um so i can properly update that save it let it rerun and i know my test ran properly fantastic so real easy to see and now i also have check this out so i have um because right i have tests now in the project i can actually click and run just these tests if i'd like right here directly and it will tell me just that one test if it ran or i can debug through and actually see what it's doing in visual studio code by clicking debug test with a break point here and it will stop when it hits that line and i'll be able to see exactly what's going on inside my code right it's going through loading up there we are i'm stopped at this line there's my order object i can open that up and i can see there's the total cost there's the coupon code and i can let that run through right all kinds of great things you can do with your tools um and let's make sure that right let's add another test here that says um when save 10 is when let's say when no code is used um should not grant savings right so no coupon code apply the coupon the coupon savings should be zero um i'm going to save that and let it rerun my.net watch is still running over here and now i have four tests that run in eight milliseconds i know that it's properly letting it go through fantastic this this is what you want to see when you write and run unit tests quick feedback very simple to write to set up the scenarios and know exactly that you didn't break anything right because let's face it when you're in that coupon evaluator if you're over here and and right somebody does some refactoring and changes that well if i didn't have that fourth test right i wouldn't know and i see there dede thank you so much i'm i'm breaking here in just a minute um i know immediately that i broke that one test that it it's now always granting savings that's a problem right if i didn't have this right so that brings up a good point that not only should you test the positive of something working test the negative also test to make sure that it does properly handle a in error scenario right handle when something shouldn't work all right put that guard rail back in and i should be back to four tests running fantastic that's all the time that i have today i am out of time um let me stop the music over here i really appreciate you joining me it's um there's so much to learn there's so much that you can go into with unit tests to make your projects better and i hope you i hope you spend some time there's going to be a lot more content available around practices inside that readme if you're reading it now you'll see the notes and things that i had in there i'll finish filling that out and it'll be uploaded with the youtube recording a little bit later today probably this evening because i've got a full day of interviews that i'm going to be conducting for dot net con friends if you're watching us here on youtube i really appreciate you watching i hope you check out the rest of the fritz and friends the c-sharp with c-sharp fritz video series here and i will see you next monday where we'll be talking about working with database take care
Info
Channel: dotNET
Views: 3,503
Rating: undefined out of 5
Keywords: .NET, .net core, c#, csharp, c sharp, test, unit test, sdk
Id: HQT1GOA3hg8
Channel Id: undefined
Length: 114min 11sec (6851 seconds)
Published: Wed Nov 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.