简体   繁体   中英

Does running unit test need to run the Main method in the code to be tested?

In C#, for unit testing only, do I need to write a Main method in the code to be tested?

I found that without Main method, rebuilding the code to be tested and the testing code will fail.

Is it correct that running unit test doesn't run Main method in the code to be tested? If yes, why does it fail to build without Main method?

Note that I am experimenting inside Visual Studio IDE, and the unit testing uses Visual Studio's unittest library. If without using Visual Studio IDE, will the rebuilding succeed?

No, unit tests don't need Main to run and unless your test explicitly tests Main the Main method will not run during Unit tests.

Why your build fails - your main project is likely exe and not class library - so indeed it needs Main .

Fix - add Main or change project to "class library"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM