 There we go. This is Tony Celeron. Hey, so good to see you welcome Thank you guys and Congratulations, right? I think congratulations are in order as well. You're a new Microsoft employee. Aren't you relative? Yes Welcome, that's exciting. So exciting and today you're gonna talk to us about cross-platform unit testing Yes, I will fantastic The The conference is yours take it away, sir Awesome. Thanks guys. Also nice to see you guys again after a while All right here everyone I'm Tony. I'm from Lagos, Nigeria. I'm currently a software engineer at Microsoft working on the Azure mixed reality cloud services So my talk today is titled exploring cross-platform unit testing tools for dotnet call and And I think this is personally important because since the gradual shift from dotnet framework Which has notably been proprietary and windows only there's kind of been a need to really understand how How unit testing looks like for this new world of dotnet core cross-platform and open source and stuff so today I'm gonna be working through a bunch of different unit testing tools and We're gonna you know, I'm gonna highlight a bunch of things that have been a bunch of tools slash products that have been proprietary and close source and only An exclusive to net framework and I'm gonna provide alternatives that are cross-platform and some of which will be open source So to get started we're gonna talk about testing frameworks. I think this is something that is I mean I think everyone knows about testing frameworks typically, you know They're a bunch of different tools in one But I think the primary use case of them is basically to run unit tests and write unit tests So they provide a bunch of different facilities from test runners to Some of them actually do include assertion frameworks out of the box So these are the pretty much the most popular testing tools in the dotnet ecosystem as you can see We're actually doing pretty good here cause Pretty much all these tools Cross-platform, they're free as well as open source So we start with X unit and unit MS test and fixing So fixie might not be so popular to a lot of people But I'm going to show you a bunch of quick sample about what fixie looks like But it's actually a convention based unit testing framework that is great for people who are For developers who really like a lot of minimalism because it does it includes just the bed the bare minimum out of the box So this is a quick example of how unit tests and X unit unit test code file looks like Typically, it's super simple. You have a you know, you have a test method You have a you have an assertion library library out of the box and Nothing too crazy and then this is fixie. So as you can see here fixie doesn't really have any fancy Impulse that you have to bring in to not have to apply any crazy different attributes it generally uses a convention based testing approach where it looks for classes that end with on tests and And basically just once the tests are done executes the methods within them and It actually depends on it doesn't come out of the box within the session library. So here we usually I will talk about surely later on in this presentation So next up are assertion libraries So assertion libraries allow you to assert the various things So basically assert that your various conditions in your tests have been met For a lot of people there is no you need to actually have a separate assertion library because most unit test frameworks kind of come with those out of the box like we saw with Like we saw here with X unit that has an assert. Equal. However, we do have a whole class of tools that have purely focused on Allowing us to easily accept that various conditions are met And most of these tools differ in the fact that they kind of introduce different user experiences. So here here to Assertion exclusive Testing libraries. So you've got few of the assertions and I'm sure they're all they're both cross-platform They are both free to use and they're also open source, which I think you might bring in is a It's a big win. So this is how it should be looks like as you can see it's It works by providing extension methods on basically objects. So when you call Calculator that add method for example, you do not need to you know Pass that to another method that says that that kind of does the assertion for you you can basically just call the That should be extension method on on the return and it will be able to check that real quick Same here with the second plate, which is a person that first name that should be one of so I think surely is actually pretty neat cause it Kind of gives us a very natural way to To look at a certain that various conditions in our tests are met Kind of feels a bit more natural than you know doing the whole assert that equals, but hey choose your poison And then mocking frameworks, I think pretty much every programming language Every programing platform out there has a concept of mocking which kind of lets us Basically let us mock different parts of Of our application in a situation where we do not want like the actual team being done So for example examples of things you would mock would be database calls network calls because Traditionally unit tests shouldn't necessarily be making be changing like global states or changing like database entries and what not so yeah, we have we have a bunch of unit of Mocking frameworks that I've been around for quite a while. I think the second one here mock has been is I think the oldest and Kind of the most popular We're also lucky that all of them are cross-platform. They are free and they're open source So mark is actually pretty good It's I as far as I'm concerned used by a lot of people including companies and indeed developers Fake it easy is another one that is also quite intuitive to use and then there's also and substitute I haven't played around and substitute a lot, but Yeah, I mean you can Take a look at all three of them and figure out which one kind of feature Best use case, but so far found more to be the most The most diverse in functionality. So here's a simple example So here we have an interface called I fool that just Defines a single method got get called on get counts so in a situation where we would like to Test and test the I fool Interface or we would like to test the method that takes the I fool interface as as an argument We can choose to mark the I fool interface as opposed to using a concrete class In a situation where I fool is kind of like a database client. Let's say I DB client You want to mark that because you wouldn't want to pass in a concrete DB client class because you do not only need your unit tests Making changes to the database So this is a pretty simple you see the setup for the I fool is actually quite straightforward and then you can tell it I should always return number 23 and then Calling that object and it lets us access the actual concrete object that was generated behind the scenes So mark here mark actually does a whole bunch of like co-generation One type co-generation to actually achieve this And then yeah, so anytime you call a fool to get counts it always returns 23 because that's what we Defined as the mock behavior The next one is isolation frameworks isolation framework can be thought of as mocking frameworks plus plus I guess because typically mocking frameworks are limited by the fact that they can only mock Things that can be extended By the regular c-shop language. So for example, mocking frameworks are able to mock Interfaces they are able to mock virtual methods. However, when it comes to things like static methods concrete classes sealed classes and whatnot Working working frameworks force shots and that kind of that's kind of where isolation frameworks take things from So an isolation framework allows us to mock pretty much anything in In your code, you could mock static classes. You could mock non virtual methods You could mock sealed classes as well. Heck you could even mock things that you didn't write Assemblies that she didn't like you have no access to the source code for and mock isolation frameworks are kind of controversial However, there's been a bunch that have been around for quite some time I think the very first on the scene was Microsoft fix and that was created by Microsoft's ourselves um, and then you have a proprietary Tools like just mock and type mock isolator so all of those uh pretty much dot net framework only and I think by definition they Only work on windows and then you've got smocks and poles. So smocks and poles open source They're free to use however smocks is Smocks is windows only Pose is actually cross platform Pose is a product of my creation. However, I no longer actively maintain it. So um, it might not be I wouldn't advise anyone to go use it. So we're kind of limited to Windows for now if you want to do anything relating to mocking of static classes and Yeah And then here's a very simple example using pose just to give you an idea of how Isolation frameworks work So here, um, you can see we create a shim that says we would like to create a shim for the for all calls to date time dot now And um, what happens here is we're simply saying whenever there's a call to date time dot now You should always return. Um, the the result of new date time 2004 So what happens is um in that isolates block that you can see on the next line Whenever there's a call in any of the methods in that block or it's a recursive thing. So in the methods they call methods that those ones call All um calls to date time dot now return a very single the same dates every time and you can imagine this This being useful in situations where you're trying to test out subscription um Trying to test out that subscription code to say, you know, let's make sure that when Date time the date is this the subscription is appropriately cancelled the the user is appropriately reviewed and stuff But um, yeah, like I said controversial It should a lot of times try your best to use interfaces for things you'd like to extend And then the next one is fake data generators. So these are actually particularly important. I'll say they're one of the I'll say they're actually one of the most important parts of unit testing because a lot of times you want What's an adequately randomized? um Data set generated that allows you to test multiple different Cases as opposed to you manually providing the data yourself Because chances are you might not provide the data that's um, you might not cover all the edge cases So fake data generators kind of help with that here. We have a bunch of we have four of them and all of these are I mean except faker.net most of this year are Class platform they're free and they're open source So here's an example of uh using gen four So here you can see we were able to we have the person class and they were able to create A single person we were able to create a list of people and what happens behind the scenes is that uh gen four basically just um Generates different uh generates data or fusing the first and last name properties Of um the person class. So you don't you know, you don't have to so if you wanted to test a situation where You'd like to send in I don't know a list of a thousand people You don't now you no longer need to like write code to create All of them or figure out a good way to run them in general of them Genful uh fake data generators will kind of Do that all of that for you And then the final one I think is code coverage. I think this has been a huge bone of contention for a while because um Most of the code coverage tools around the block were Kind of still.net framework exclusive and windows by extension So here we see a bunch of a few ones we see um I think the the first three are To our windows only open cover actually works with dotnet core. However um And it's also open source, which is a good thing. However, it's only works with dotnet core on windows Same with um vs code coverage and dot cover. So dot cover is proprietary by made by check brains and vs code coverage, I mean we have to We have to get a visual studio license for that And then the last three are open source fully cross-platform and absolutely free um coverage alternatives So here we see cover light or cover and And mini cover So you can take a look at those for your For your dotnet core core coverage needs and like mac, cullinux and whatnot So um, I think I'm going to do a quick demo To show how all these things kind of come together So Let's see Okay So here, um, I have a very simple I have a very simple person class It just does three things. It has the first name the last name and has a full name that is expected to return All of this and then in my tests. So in my tests, I'm currently using x units as the See over here x unit as my testing framework I am using Jennifer as my I'm using Jennifer as my data generator and I'm using surely as my Assertion library So this is a very simple Contribute example of how to kind of use both of them. So here we have first thing we do a generate new person um And then the next step is we figure out that what we call the full name on the Person object. We're able to get the actual full name in the format that we expected Which is basically which is first space Space last and then this is how we test them Full names. So yeah, this kind of shows us the best way to Kind of shows us like the combination of surely Jennifer and unit and x units So in a situation where we would love to like figure out our code coverage for this for this test What we can do is if you look over here, you can see I have this thing called the covalent or collector And what happens here is um, we're including the covalent Code coverage tool in our project And this allows us to I'm gonna Show it So running our test normally I think we should all pass with As quickly as possible 18 waiting waiting waiting Okay, that my test seems to be taking a while which is kind of weird Or I can just come here and Just run it to show Oh, there we go To get pretty little time So let's just hold on for it. There we go Now with the inclusion of the covalent collector, we're able to do this to get our So to get a code coverage for yourself, you can just do the dotnet test dash dash collect And then do the use the collector id which is called xbox code coverage And then doing this We'll Actually we'll run the test as usual and then half covalent collect the coverage afterwards So this is Wrapping up real quick So as you can see here It has a says um says it's got attachments So if you go over here and come down here If you can see the cold coverage output in perpetual format, perpetual format is actually very pretty standard format and supported by most um continuous integration um systems Including um azure develops So I know a lot of people would love to like see be able to get um like a summary Like see at a glance what their code coverage is right here in the console so um We can switch that a little bit. So I just want to mention before I switch over that When the new dotnet core 3.0 cli when you do a dotnet when you create a new ms test unit test project Covalent or collectors already kind of comes out of the box with it um But if you're using any an alternative testing for a mock then you want to kind of include it directly from nugget So i'm going to remove this real quick Just to demonstrate something and then I'm going to add a dotnet package Covalent ms build So this is going to install the covalent ms build integration package And also that is fully restored. I will Demonstrate how to Use it as well So we're just waiting for it now Let me close this I don't need to restrict you installing installing there we go and then Now that is installed we can do a dotnet test so because it's ms build integrated you'd have to use the ms build dash p and then Tell it to collect coverage And what would happen is you basically do the same thing as before It will run your tests But then instead of just spitting out The results to A file it would actually show you So yeah, it'll show you the summary right right here in your console So here we have 100% which is kind of pretty awesome And it also still spits out a result file in its own json format But you you have the option of you know changing different from using different formats from open coverage to copy to it as well And um, yeah, I think That would be it for my For my demo, I don't have much to demo and also run out of time But yeah, does anyone have any questions? Wow, you covered a lot Questions anyone That yeah, that was a lot. Tony. Oh my gosh. So many different We were we were talking about gen foo. Yeah gen foo is really cool. Um, I've Have you looked at some other? I don't I didn't hear if you mentioned like bogus and there's there's some other Um, you know what I think bogus. Let me go Back I am well available. Yeah. Yeah bogus is right here. Oh, okay. I am well available bogus. Yeah Yeah, I remember I had a chat with uh maintainer sometime last year He's uh, it's actually pretty cool. I'm just a bit more funny of a gen foo, which is why I'm beauty Oh, by the way, I should mention that everyone can you know go actually check out these tools for themselves and pick out Kind of what they actually want I just had to pick one of out of a hat It's a good list. How did you personally get interested? This is something you've you've really like A field you understand very deeply. How did you get? You know to be an expert in this Well, I mean first of all, I wouldn't consider myself to be an expert, but um, I guess, um It was when I needed to I was actually making the switch from dot net framework to dot net core I was an early adopter of dot net core Um, isn't it since um, when he was in beta one and um I needed to do a bunch of like code coverage a bunch of testing related things And I remember kind of hit a bunch of roadblocks So some of the the cross-platform open source free, um testing tools I presented here today I actually created a few of them. I know about just That's the man. Yeah coverlets pretty fair. Is that maybe one of your most famous ones? He was coverlet Oh, yes, the most famous In the chat room, um osbob is asking what what what's the modern alternative for fakes? Uh, it used to be pose. However pose doesn't have a It's not particularly easy to maintain of course Um Changes in the runtime because he uses a lot of undocumented runtime features So, um, yeah, I can't really say I mean I'd say you could you could try out smocks But it's windows only so if you're looking for a cross-platform alternative. I think for now the The community is a bit out of luck with that At least until I can get back home to working on pose Okay, so And osbob follows up here in the chat room saying looking for just windows So it looks like smocks might be a good alternative for you osbob. Yeah. Yeah Smocks will be a good alternative. It's just windows is looking at Okay, I have a slightly unrelated question. See you're new at microsoft and you're working in in a new Microsoft office there, right? Yep, the adc diggers start that for african developments and That is so cool. How many people are are working there? Um, currently we're about, uh, I think 10 or 11 And we're working with the cognition team. So the team responsible for The hollow lens and all the azure mixed reality services or whatnot We have a new team starting Next I think next month and they'll be working with the windows team as well. So even we're growing Baby steps And one other question Excuse me one other question along that line For our viewers that are in africa that are that are new to dot net Want to get hooked up with the community? Is it what what's a good idea for them to do? Are there meetups? Well, I mean we do have uh, it depends on what part of africa they are But it's a bit hard a bunch of We do have a bunch of Like we do have a bunch of dot net foundation meetups Yeah, I know we have one here in Lagos. We have a few in Nairobi as well So, um, yeah, I think they could either if they could get across other african countries or they could I mean Upstanding their own meetups and I think there's this new series that's called handsome man and I can draw a kind of working on about like introductions like see sharp on dot net call They should also watch that because I think I think it's pretty cool. Very cool. All right All right, thanks so much. Tony. We really appreciate having you joining us this year for dot net con Uh, thanks for having me. Oh, and this is an important slide here. Let's keep this up just for a second here So that's that's his github and and all the contact information So yep, so if you have questions if you'd like to Is there a way I can like I don't know share the slides or do we just assume that one is going to work? uh, yeah, we can We can definitely share some slides share slides. Um, you can send them to me You have my email address here because you're on the Internal and we'll work on getting those links posted Uh after the event Awesome. All right. Thanks guys. All right. Thanks so much. Tony. Thanks so much. Tony. Take care. Uh, all right See ya. All right. Goodbye, Tony