How do you test methods as you are coding? - c#

Currently using VS2010 Ultimate. In a prior version of VS there was a utility that you could use to test methods while you were coding (Some sort of test bench, I think). The benefit of this, was in the case of a large web app, you didn't need to spin up and wait for the entire application to be ready before testing your method.
What sorts of tests / jigs do you use to test static and instance methods in your apps w/o firing up the entire application?

LINQPad works great for running small bits of code.

Using the MSTest Project and have it reference your app you are trying to test.

VS2010 comes with a system that allow you made unit tests... and let you run each tests individually if you want. You may be interested in this channel9 video about unit testing with vs2010: http://channel9.msdn.com/blogs/visualstudio/test-driven-development-with-visual-studio-2010

I think the question here is how to start your method from anywhere, to have an entry point to quickly test something.
If I remember correctly, TestDriven.Net allowed you to do this.

Related

Specflow in c# project and Nunit unit test

I would like to say thanks to everyone here ,because i am learning many things from stackoverflow...every time i am living with new energy ..its all here.
yes of course ,i need oxygen to breath again...all helping hands i want ...may be my questions are silly but really day to day am brushing and growing as kid of stackoverflow school.
i have little knowledge on working with c# project and unit test(Nunit/xunit)..where i use Nunit test to debug my code which i have written in c# project.
now i got a new challenge to do WCF service automation in BDD approach using specflow.
i tried by adding spec-flow features and steps for couple of scenarios into Nunit unit test project and was able to run and identified tests.
where as if i create same set of features and steps ,not identified any test and not run anything .even not recognized [Test] or [TestFixture].
let me explain what i have in mind, i want create all spec-flow features and steps in c# project and should be run this solution through nunit.exe/console by passing "REGRESSION" or "SMOKE" tests(by configuring environment details) in command prompt.
and for debugging purpose i want call these spec-flow features (in c# project) from my unit test project...is this approach possible?
if possible ,please let me know with few sample and possible explanation/details to implement.
Issue: 1. I am not able to run any Spec-flow feature from c# project (since not test is identified)
2. Don't know how to call feature from unit test to do debug or to test my code before i integrate to team-city.
please consider all these my basic doubts and make me to learn.
Thanks In Advance,
In regards to your issue 1 and 2, I guess you are not able to see features in runlist and hence not able to execute.
if you are not able to discover specflow features in run list. You can try installing "Specflow for Visual Studio 2013" in extensions and updates. Please note this is in addition to "Specflow" package that you might have installed.
You will see such features that can be added and you will have to add steps implementation. Its not related or part of Unit tests.

Simple console app tests

I'm currently doing some trainee work & looking for a bit of advice.
I've been set a pretty simple task of writing some unit tests for a service application in C#. The service mainly has methods which query and look up and SQL database for various things. I've been asked to write unit tests for the majority of these methods and other things like simply checking the connection and stuff. I've been told to keep it simple and probably just write the tests in a Console application. I'm wondering what the best way to go about this would be?
Would simply calling the methods from a console app with hardcoded input be suitable? Then just check what the output is & write whether is passes in the console? Or is this too simple and nasty?
You can run both MSTest and NUnit tests from the command line and in my opinion that would be far more preferable than writing your own test runner from scratch. Concentrate on writing quality tests, not the scaffolding required to execute them and deliver the results.
I would suggest it's as simple to do it "properly" as it is to craft your own solution.
Note though that tests connecting to the database are integration tests, not unit test.

Revit API Code Unit Testing

Is it possible to create unit tests of my own Revit API code that interacts with a Revit DB model without having Revit running.
If so, how might this be achieved.
I've been involved with some of the threads that are mentioned.
I've even worked on a project that had a very well developed unit testing framework for Revit (which unfortunately I can't share).
But here's what I can tell you about the approach:
You need to build your own basic framework for executing tests.
It runs as an Addin inside of Revit (you have to start Revit, choose the Addin, and then choose the test harness assembly to run).
After that, it's much like xUnit, MSTest, etc.
I think we had some additional test attributes that even pointed each test to a particular test model.
The hassle in the whole thing is that you have to start Revit up, which as you know can take some time (and makes the process a lot less convenient than using unit testing in traditional development). Until Autodesk decides to open things up more, that's what you're stuck with (unless you go with the "Revit Python Shell" approach... I guess I was referring only to traditional .NET languages).
Good Luck...
The Dynamo project includes the Dynamo Revit Test Framework, which is allows you to run NUnit tests on the Revit API. It handles the generation of journal files to create individual Revit sessions to tests in isolation (tests can also be run together as a fixture in one revit session). It's geared towards Dynamo presently, but would be easy to adapt for non-Dynamo use. And it is open source as part of the Dynamo project.
Also, if you make improvements to the source, please feel free to make a pull request. We're always looking for good contributions.
You could abstract your API code into an interface, then implement it on a set of mock objects to use for testing:
Unit Testing: Mock Objects to the Rescue!
I'm not sure you find more about Revit unit testing than described here
http://forums.augi.com/showthread.php?98536-Unit-testing-with-Revit-API
and here
http://darenatwork.blogspot.com/2010/11/unit-testing-revit-plugins_10.html
Or may be you find your own way how to use unit tesing with Revit API.
Jeremy Tammik wrote a blog post on this topic
http://thebuildingcoder.typepad.com/blog/2013/07/revit-add-in-unit-testing.html

How to unit test control library?

I program simple control library that checks user input from text and format the input.I want to make a unit test.How can I do that ?
I would take a look at this SO question. There are some good responses there. Marek Grzenkowicz's CodeProject article has some information on unit testing a TextBox he developed.
Edit:
Testing the UI can be a challenge and I generally try to pull as much out of the UI as I can and put it into a more testable class. Obviously, you want your unit tests to be run without any need for user interaction, so if your class method takes in the input string and formats it, you can write a test (using NUnit, MS Test, etc) to provide the input and test the actual output against the expected results.
I'd point you at NUnit and see if that does what you need. It is easy enough, also, to create New tests in Visual Studio under the Test->New Test... menu item. I know this is available in the VS 2008 Professional and assume it is available in other versions too.
Also, I'd take a look at this SO question about NUnit examples. There are some links to some examples there too.

System Testing a desktop application

I've got a desktop application written in C# created using VS2008 Pro and unit tested with Nunit framework and Testdriven.net plugin for VS2008. I need to conduct system testing on the application.
I've previously done web based system tests using Bad Boy and Selenium plugin for Firefox, but I'm new to Visual Studio and C#.
I would appreciate if someone could share their advice regarding this.
System testing will likely need to be done via the UI. This gives you two options:
1) You can manually conduct the test cases by clicking on elements.
2) You can automate the test cases by programming against the UI. There are plenty of commercial tools to do this or you can use a programming framework like the Microsoft UI Automation Framework. These tend to use the accessibility APIs built into Windows to access your UI.
Whether you go the manual or automated route depends on how many times you will be running the tests. If you are just going to run them once or twice, don't spend the time automating. You will never earn it back. If you are going to run them often, automating can be very handy.
A word of caution: Automating the UI isn't hard, but it is very brittle. If the application is changing a lot, the tests will require a lot of maintenance.
As Thomas Owens commented on your question, first you must decide what kind of system testing you want to do. But assuming you want start with Functional System Tests. Prepare use cases you want to automate. Than you must find proper tool.
Just for start:
AtoIT – is not test atomization tool but it lets automate some tasks. So you could record/script use cases. Not really recommended, but can be done.
HP QuickTestPro – easily can be done with this tool via recording/scripting but it is expensive, so maybe not worth it for personal use.
IBM Robot – as HP QTP.
Powershell – you could write scripts in powershell and execute them. If you would use dedicated ide-like tools for powershell you could record test also. I did some web automation via powershell and it worked. With a bit of work probably you could script around your desktop app.
And the best would be to try different tools, and use one that suits you best. Try this link and this link.
System tests usually have use cases, end to end scenarios and other scripted functions that real people execute. These are the tests that don't lend themselves well to automation as they are asking your unit-tested cogs to work with each other. You might have great unit tests for your "nuts" and your "wrenches" but only a comprehensive system test will let you know if you have the right sized wrench for the nut at hand, how to select/return it from/to the drawer, etc.
In short - manual tests.
If you're willing to put money down, you could look at something like TestComplete.
Although I haven't really used it yet (our company just bought it), it seems quite nice. You can record clicks and keypresses and stuff, define success criteria, and it will replay the test for you later. It appears to be quite smart about UI changes - it remembers which button you clicked, not just the (x,y) of each click.
It's scriptable, or drag-and-drop programmable.
I'm not affiliated in any way, and this is not an endorsement, because I haven't really formed an opinion of it yet.
Perhaps NUnitForms could be useful for you?

Categories