UNIT test. How to open the form in the test? - c#

There is a Windows Forms application.
When the application starts, the form "Form0" will start.
Form0.ShiwInTaskBar = False;
I want to test the application.
I completed:
- created the project "UnitTestProject1".
- prescribed a link to a solution that I will test.
When testing, the Form0 should open.
The "Form0" class does not have a "Show ()" method.
How to open the form in the test?
Form0
namespace rsh
{
public partial class Form0 : Form
{
public Form0()
{
InitializeComponent();
}
}
}
Тест
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//
using rsh;
namespace UnitTestProject1
{
[TestClass]
public class TestsStart
{
[TestMethod]
public void TestStart()
{
// Тест "Form0"
Form0 form0 = new Form0();
form0.
}
}
}

If you want to test the code:
You should be directly hitting the code as you are doing white box testing. You'll not be able to touch the UI side.
If you want to test the UI:
Please use a tool to interact with the UI of an Windows application:
Click Here..

Of course it is possible if you for your own testing
1.- In your testing project add the System.Windows.Forms reference (References section)
2.- do the below:
Form0 form0 = new Form0();
form0.ShowDialog();

Related

I added a reference to the main project, but the testing project still doesn't see the classes of main

My solution has a project that contains all the program logic.
I created a unit test project, added a reference to the main project, but still can't use classes from it to create tests.
My code:
namespace Program
{
public class Class
{
public Class()
{
///
}
public int foo()
{
///
}
}
}
My tests code:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Program; // cs0246
namespace ProgramTests
{
[TestClass]
public class ClassTests
{
[TestMethod]
public void foo_()
{
// Arrange
Class testClass; // this code also have cs0246 error
// Act
// Assert
}
}
}
In this code, using Program; underlined in red with cs0246 error. But namespace ProgramTests have the reference to Program (there is a checkmark in the reference manager). How can i fix it?
Image of Solution Explorer
Please, check the framework version you are using. Maybe Tests library has framework which is no compatible with ClassLib Framework

Program opens cmd then closes

Title says it all, it opens a command prompt then closes in 1/2 a second. i dont know why and i havent found anything on this. i just made a new c# app. Might have something to do with Main as it has no reference.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace dumb_thing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void Main(string[] args)
{
}
private void Animatetext_Click(object sender, EventArgs e)
{
Animatetext.Text = "";
Thread.Sleep(700);
Animatetext.Text = "P";
Thread.Sleep(350);
Animatetext.Text = "Pa";
Thread.Sleep(350);
Animatetext.Text = "Par";
Thread.Sleep(350);
Animatetext.Text = "Para";
Thread.Sleep(350);
Animatetext.Text = "Parad";
Thread.Sleep(350);
Animatetext.Text = "Parado";
Thread.Sleep(350);
Animatetext.Text = "Paradox";
Thread.Sleep(350);
}
}
}
It seem your codes is about to run a form application. No console application.
But if you are trying to develop a console application, the console will close automatically because there is no command to make it stay or wait the next key press. So you can add a line to make the application stay before closing the Main() method.
static void Main(string[] args)
{
//
// your coding here
//
Console.ReadLine();
}
It is not a good practice to develop a console application via form application template. Unless to get debug output.
Editted
If you want to develop a Form app, you have to remove the Main method in your form class. The Main method is only called once to start the form but you no need to add in your Form classes.
Full Example for Form
Program.cs (Here is where the Main method is placed and called only once to start the other form.)
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
And now, here is the other form classes.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Make sure to includes all library needed. This example taken from VS2019 .NET Framework Form Application template.
Other Occurs
In VS, you can set to start your project with console or not. You can set to in your Project Properties > Application Tab > Output type set to Windows Application. By default this setting will set to template setting (if form: windows application and if console: console application).
For best practice in form application, use the debugger console and set output using Debug.WriteLine().

Unit tests missing output in VS2017

EDIT:
I might have found a possible lead.
Checking my Test Runs it would seem as if I have 0 completed runs (even though all my 15 tests complete). Any clues?
I've got a technical interview coming up where testing will be the main focus. I've got rather limited exposure to testing in Visual Studio and can't seem to figure out why my version (VS2017) won't display the output button when I run tests.
Since my limited exposure I've been following along a few PluralSight courses on the subject and have found a decent one covering both LINQ and VS's own unit testing framework.
This is where it should be on VS2015 (I think?):
And this is how it looks for me:
As you can see I'm missing the output button for some god forsaken reason. I've looked in multiple windows (output's debug and tests), but simply cannot see the output.
My unit test follows the instructor's structure with some small changes (like how I set up my TestContext, which follows the structure of this answer to a similar question..
This is my unit test:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ACM.Library.Test
{
[TestClass]
public class BuilderTest
{
private TestContext _testContextInstance;
public TestContext TestContext
{
get { return _testContextInstance; }
set { _testContextInstance = value; }
}
public Builder listBuilder { get; set; }
[TestInitialize]
public void TestInitialize ()
{
this.listBuilder = new Builder();
}
[TestMethod]
public void TestBuildIntegerSequence()
{
var result = listBuilder.BuildIntegerSequence();
foreach(var item in result)
{
TestContext.WriteLine(item.ToString());
}
Assert.IsNotNull(result);
}
}
}
EDIT:
Here is the function I'm testing...
public IEnumerable<int> BuildIntegerSequence()
{
var integers = Enumerable.Range(0, 10);
return integers;
}

Error: You MUST call Xamarin.Forms.Init() - Visual Studio Xamarin, unit test error

I made an android application in Visual Studio, using Xamarin. I have an emulator installed in visual studio. The applaction uses txt files, which I made on the emulator earlier (File.WriteAllText). The problem is that I can't run unit tests. DependencyService.Get throws an error.
Test
Result Message: Unable to create instance of class (...). Error: System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it..
I'm not sure where I'm wrong, and what I should do here.
There's an IPersistence class which gives the loading from file method.
public interface IPersistence
{
Task<Player[,]> Load(int x);
}
I implemented it for android in another class:
public class AndroidDataAccess : IPersistence
{
public async Task<Player[,]> Load(int x)
{
/*...*/
}
}
Unit test Code:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Model;
using Persistence;
using Xamarin.Forms;
namespace UnitTestProject1
{
[TestClass]
public class Test1
{
private Model _model;
private IPersistence _dataAccess = DependencyService.Get<IPersistence>(); //It throws an error.
[TestInitialize]
public void Initialize()
{
_model = new Model(_dataAccess);
_model.GameWon += new EventHandler<GameWonEventArgs>(Model_GameWon);
_model.GameOver += new EventHandler(Model_GameOver);
_model.FieldChanged += new EventHandler<FieldChangedEventArgs>(Model_FieldChanged);
_model.GuardChanged += new EventHandler<GuardChangedEventArgs>(Model_GuardChanged);
}
[TestMethod]
public void BasicTest10()
{
_model.NewGame(10);
Assert.AreEqual(4, _model.S);
}
DependencyService requires a Forms context to operate. This is normally created when creating the MainActivity for Android or the AppDelegate for iOS. For your test I would recommend creating a test fixture for each platform and then create a method that manually initialises dataAccess based on the platform.
[TestFixture(Platform.Android)]
[TestFixture(Platform.iOS)]
public class Test1 {
...
...
private IPersistence InitialisePersistance(Platform platform) { ... }
}

Problem to open REPL using Xamarin Test Cloud

I'm beginner in Xamarin Test Cloud and I want to write tests for Xamarin Test Cloud.
I have Xamarin UITests in my solution and I tried to launch REPL, but UITest REPL window didn't open.
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.Queries;
namespace MurakamiKiev.UITests
{
[TestFixture]
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.StartApp ();
}
[Test]
public void TestLaunch ()
{
app.Repl();
}
}
}
Where is the error?
Also, what I need to write to launch specified activity?
If you don't have the application source code in the same solution then you'll need to specify the prebuilt app by pointing to it via a full path.
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.ApkFile("<path-as-string>").StartApp ();
}

Categories