0

This is the first time I'm trying to do unit tests so I'm pretty sure it's on me, but for some reason I'm not able to make them work.

I have a project, with some classes. The project is in 4.7.2 Framework .Net.

In the solution, I added a new project. I chose MStest for 4.7.2 framework .Net too.

enter image description here

Now, I added the references via Add references.

enter image description here

enter image description here

But even that, for some reason in the UnitTest.cs file, I can't call any classes. I can't even call the main one program.cs, it looks like it can't recognize the namespace. What's going on? What am I doing wrong?

Acording to Microsoft docs

 myNameSpace.Program.Main()

it should work, but it doesn't. There's not even showed the project's namespace

Sharki
  • 404
  • 4
  • 23
  • 1
    Anything being tested must have the `public` modifier so they are visible to the test project. `internal` can be tested if the [InternalsVisibleToAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute) is added to the assembly being tested. Are you sure the classes you're testing are `public`? – Metro Smurf Oct 18 '21 at 00:41
  • They weren't public, now I can see the namespace so it seems to work. Just for curiosity; is it okay for classes to be public? Also, in this case the class wasn't preceded by the keyword public nor private, how is this type of class treated? Thanks in advance. – Sharki Oct 18 '21 at 00:45
  • Make sure you're referencing the MSBuild NuGet packages that match the version of Visual Studio that you're using. Also ensure you have the Test Adapter packages referenced too. – Dai Oct 18 '21 at 00:58
  • 1
    "is it okay for classes to be public?" - yes. and no. Depends on your use case. As a new developer, make them `public` until you *know* why they should be anything else. By default, anything without a modifier (public, protected, internal, private) is `internal`. Here's some reading on SO to get you started: https://stackoverflow.com/questions/614818 I'd recommend you spend some time learning the basics of C# development before you get much further. – Metro Smurf Oct 18 '21 at 01:06
  • @MetroSmurf Thanks, last thing I'll ask I promise, when people are testing, is it legit to change the attributes from only getters to also setters to change the values in the object property and test faster/easier? Of course leaving it afterwards as it was originally. What I'm asking is if people tend to do this and if is it kinda valid. – Sharki Oct 18 '21 at 01:11
  • 1
    @Sharki - the target being tested (class, property, method, etc...) should never be modified simply to accommodate unit testing; doing so is a code-smell of either a poor design or a poor testing strategy. If a property is getter-only, then a new class should be instantiated with the expected value; never change the signature of a property simply to accommodate testing. – Metro Smurf Oct 18 '21 at 14:36
  • I see, thanks a lot! – Sharki Oct 18 '21 at 16:04

0 Answers0