Unit Testing without a main static method? - c#

I am starting to go through a tutorial on Unit Testing. I am using Visual Studio 2019 and I am trying to run the projects/ tests but keep getting these errors,
CS5001 Program does not contain a static 'Main' method suitable for an entry point
CS006 Metadata file 'C:(filepath)... .exe' could not be found
I know neither class has a main static method but the tutorial I am following along with doesn't either so I am not sure what the issue is. I have searched Stack Overflow and Google but can only find answers going over my head, any idea what how to fix this?
It is pretty simple code as I am just at the beginning so I think the problem is something else but am not sure (obviously)
UPDATE:
Here is a screen shot of the structure of my solution:
structure of solution
The original project was a Console application, the I added a Test project to the solution.

Test project are not applications and are normally run using a test runner.
Run you tests from VS's Test Explorer
Option B. Run the test directly from its code.

It sounds like you've written your unit tests inside a Console Application template, removed the Program class's Main() method and hit F5 to run your unit tests.
You don't F5 unit tests, you run them from a test runner. Right-click any of your test methods and choose something like "Run Unit Tests" to show that test runner. Or click Test -> Test Explorer to see all discovered tests. See also Run unit tests in "Get started with unit testing".
If that works, create a new project that's actually a Unit Test Project so you at least get a fancy project icon, and move your test code into that project.

Related

Xamarin - Cross Platform Unit Testing #2

This is a common question, it's really quite astounding to me how difficult such a simple task is turning out to be ...
I have a Visual Studio (formerly Xamarin) C# cross-platform library. It has some code in the shared "abstract" library, and a few machine-specific implementation classes for Android, iOS, and UWP libraries. It also has a few automated (non-UI) unit tests that use the Microsoft.VisualStudio.TestTools.UnitTesting package. This is a great start, when Visual Studio 2017 is feeling generous it runs my unit tests and life is good.
Now I want to run the unit tests on simulators of the actual devices, from a command line (so I can run them from Jenkins and capture the output). So, for example, I want to run my unit tests on a simulator of an iPad. From various web sites I get the feeling that I can't use the Microsoft package for this, so I need to switch to either Xunit or NUnit. I gave up on Xunit earlier today, here's how NUnit went.
Go into the Unit Test project and remove the NuGet packages for Microsoft.NET.Test.Sdk, MSTest.TestFramework, and MSTest.TestAdapter.
Add NUnit instead (version 3.7.0).
Go into each CS file, change to "using NUnit.Framework", change [TestMethod] to [Test], and [TestClass] to [TestFixture].
Project:Manage NuGet Packages, add the NUnit3TestAdapter.
Test Explorer: Run All
At this point I get this weird error for the UWP machine-specific library:
The "XamlCTask" task could not be initialized with its input parameters
The "DebugType" parameter is not supported by the "XamlCTask" task. Verify the parameter exists on the task, and it is a settable public instance property.
That's a really strange message, and if I Rebuild the UWP library, it rebuilds without errors. Not sure why it's building the UWP library as part of the Unit Tests, the only thing listed under "Projects" is the abstract (non-machine-specific) library.
Also strange that building the Unit Test library works, it's only when I do the Test Explorer: Run All that I get these errors.
OK fine, it's a known error.
Close VS2017, delete bin and obj folders, reopen, no better. Close VS2017, hack the Xamarin.form.targets as listed. Same error. Realize error is in the global Xamarin.Forms.targets, not the one in my solution. Hack it. Close & reopen solution, rebuild. No error this time!
Test Explorer: Run all. Nothing. Output:Build shows no failures, Output:Tests is completely empty. Close & reopen solution. Get new errors:
[Failure] Could not find file 'C:\Workspace\PopComm.iOS\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs'.
[Failure] Could not find file 'C:\Workspace\PopComm.iOS\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs'.
[Failure] Could not find file 'C:\Workspace\PopComm.iOS\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs'.
Test Explorer: Run all. This time get more interesting errors:
------ Discover test started ------
NUnit Adapter 3.7.0.0: Test discovery starting
Assembly contains no NUnit 3.0 tests:
C:\Workspace\PopComm.iOS\bin\iPhone\Debug\PopComm.dll
NUnit Adapter 3.7.0.0: Test discovery complete
========== Discover test finished: 0 found (0:00:00.0530024) ==========
Of course that assembly doesn't contain any unit tests! It's not my unit test project!
Sigh.
OK, create a new project of type "NUnit 3 Unit Test Project". Move my test classes into this new project and add a dependency to my non-machine-specific library.
Test Explorer: Run all - Tests! There are tests! And they even run successfully.
Close VS2017, open again, Test Explorer: Run all. No tests. Output:Tests is empty.
Close project, delete all bin and obj folders, Rebuild Solution, Test Explorer: Run All. No tests.
Push code to git, pull on the Mac.
Open solution in Visual Studio for the Mac.
Use the mostly-hidden View:Test feature to bring up the Unit Tests window, run the tests. Tests found and run. Yippee!
Now I want to run my unit tests on an iPad simulator, not inside VSMac.
Add a new project to my solution, type iOS:Tests:Unified API:Unit Test App.
Wizard asks what is the minimum iOS version I want to support. I figure I should match the iOS version that my machine-specific library targets, so I go looking in the VSMac options for the project. The iOS target version isn't shown anywhere. Use the default (10.3) for the Unit Test App.
Do I have to copy all of my unit test CS files into this new app now? Or can I somehow reference my cross-platform unit test project in this Unit Test App?
Pushing everything and pulling on the Windows machine, I try adding a reference to my unit test library but get an error:
The project 'NUnit.Tests' cannot be referenced. The referenced project is targeted to a different framework family (.NETFramwork)
Hum. I wonder what framework the new NUnit.Tests.iOS application is targeting. Check its options - it doesn't say anywhere. Remove the reference from the Unit Test App back to the unit test files, and just copy the unit test source files over (non-optimal).
Run the application in the iPad simulator, it finds & runs the tests, but I don't know how to (1) run it from the command line, and (2) capture the unit test statuses in a file.
So after all this, I still don't know how to run my unit tests (1) from the command line, (2) on an iPad simulator, and (3) capturing the output in a text file.

Host Unit Testing Framework in our Application? (Nunit, xUnit, MsTest)

We are developing and maintaining a legacy winforms application. The application uses a domain model which ist quite separate from the GUI layer, but because of many dependecies, it is hard to unit test. At least we have a build and an installation script which we can use to automatically build and deploy the application.
Now if we could only have some automated system tests...
We came up with the following idea:
First, we could write our tests in script files like this:
public class Test
{
public void TestSaveACustomer()
{
var c = new Customer();
c.Name="Miller";
Mapper.Save(c);
Assert.IsTrue(c.CustomerId >0);
}
}
Then, after building and deploying the application, we could run the scripts with a script runner that is baked into our application binary. We imagine we could use the following commandline
myapp.exe /runTest c:\myScript.cs
We could start to build our own Testing framework, wouldn't that be cool? ... er... no. It would be much less work if we could use an existing testing framework like nUnit, xUnit or MSTest from our application .
I guess I need to
Start a UnitTestRunner
Compile the Script
tell the runner to run the tests in the compiled script assembly.
I have no idea where to start, because I have only used UnitTests from VisualStudio.
Would that be possible with any existing testing framework? Which are the main classes I would use?
Here a existing answer how to run NUnit tests by yourself.
Question is, what does you prevent from running the tests regularly?
To get to run myApp.exe c:\test\test.cs you would also need to compile the test, then run it. You still need to compile it against your app?
Can you debug your application from let's say Visual Studio? Right? So, you could add a 'sub project', which points to the main project for the dependencies and your code. Then you add your tests there, using NUnit regularly.
Or what does prevent that?

Tests succeed when run from Test View but fail when run from test list editor or command line

I have a test suite, comprised of both unit tests and integration tests, in a project using C# on .NET 4.0 with Visual Studio 2010. The test suite uses MSTest. When I run all tests in solution (either by hitting the button in the testing toolbar or using the Ctrl-R A shortcut chord) all of the tests, integration and unit, pass successfully.
When I either attempt to run the same tests from the command line with mstest (explicitly using the only .testsettings file present) or attempt to run them from the Test List Editor or using the .vsmdi file the integration tests fail.
The integration tests test the UI and so have dependencies on deployment items and such, whereas the unit tests do not. However, I cannot seem to pin down what is actually different between these two methods of running the tests.
When I inspect the appropriate Out directories from the test run, not all of the files are present.
What would cause some of the files that deploy correctly in one situation from Visual Studio to not deploy correctly in another?
The static content started being copied shortly after I wrote the comments above. The other major issue I ran into was that the integration test project referenced libraries that were dependencies of the system-under-test (with copy-local set to true) in order to ensure that the DLLs would be present when they were needed. For some reason, these stubbornly refused to copy when the tests were run through Test List or mstest.
What I eventually did to work around it was include [DeploymentItem] attributes for the DLLs that I needed. This got things working no matter how the tests were run. What I am still unclear on, that may have answered the underlying solution, or provided a better solution, is how Test View/mstest differ from the regular test runner (assuming that the correct .settings file was passed to mstest.). I'm putting these notes/workarounds in an answer, but I'll leave the question open in case anyone can address the underlying cause for how the different test execution paths differ.

Can I debug while running a VS Unit Test?

I want to unit test a user component which use custom events. when doing this without using VS Unit test Framework debug.assert succeed, when doing the same thing with VS Unit Test Framework, assert fails this is illogical so I want to debug while doing Unit test with VS framework. But It seems I can't because code never pause at debug point that I set.
So is there a way to force it to pause at debug point in that case ?
Update: I mean not in the Test Project itself but in the Targeted Project while running the Test Project.
Yes, with the Visual Studio Test project, you can set breakpoints and step through your code.
Be sure to run your tests with the Debug menu -- Tests -> Debug -> Tests in Current Context.
For any given test class or test method, use the keyboard shortcut Ctrl-R Ctrl-T.
When your cursor is in a method (either a test method, or the code covered by the test), it'll break on any breakpoints in that method, or any breakpoints anywhere in the code execution path. If your cursor is on the class, or in between test methods, it'll run all tests in the class. Breakpoints will be hit here as above.
The answer by P. Campbell is not exactly right. If you start debugging a project (such as a WCF services site), it is impossible to start any unit tests, debug mode or not. The options to do so are simply grayed out in Visual Studio 2012.
This means you simply cannot debug out-of-process code from a unit test. You can only debug code that was directly called in process by the unit test.
This is a major problem in VS2012 that they need to fix now.
In VS2015, select:
Test->Debug->All Tests
Or you can highlight a specific test in the editor and select
Test->Debug->Selected Tests
Under the Test Explorer -> Right click on the specific test case you want to debug -> From the Context Menu click Debug the selected test case option.

TestDriven.Net Only Running One Test Fixture In File

I'm using NUnit (but have also tried this with VS testing) and I'm having a problem getting TestDriven.Net to recognize and run different test fixtures in a single .cs file.
I'm trying to do a little BDD style testing. So what I have in one file is something like this:
[TestFixture]
public class when_view_is_ready : AAA
{
// setup, tests
}
[TestFixture]
public class when_something_happens : AAA
{
// setup, tests
}
When I run this in the NUnit GUI runner it sees the different test fixtures just fine and runs all the tests. When I run it via TestDriven.NET context menu and watch the Output window, it only runs the first fixture's tests. Is there a reason for this? Can this be fixed?
I think I'm running TestDriven.Net 2.0. I can't be certain; not exactly sure how to check my version.
Appreciate any help!
You can check the version of TestDriven in Help -> About in Visual Studio, or in Tools -> Addin Manager.
I can confirm that this is an issue in 3.0.2556, when running tests it runs only the first TestFixture in the file, when you right-click the file, and select "Run Tests". When you right-click on the containing directory or project, and "Run Tests", it runs both fixtures.
When using other test runners, like Resharper's testrunner, even running it on the file runs both fixtures.
Best thing to do, is probably to report a bug with TestDriven.net

Categories

Resources