Recently, I wrote some unit tests for a Web Service developed by vs. Although some test teams will do the test, it will bring convenience to the subsequent development work to do the unit test for the functional module.
The following are some problems and solutions encountered in vs unit test:
VS can use a third-party tool for unit test. I want to discuss it with the unit test project that comes with.
Question 1:
In the WEB Project, Web. config cannot be read in the unit test project. How can this problem be solved?
Solution:
1. You can create app. config for unit test and copy the content of web. config to app. config. Disadvantage: Remember to modify app. config every time you modify web. config.
2. use the app. put the config file in the bin \ debug or bin \ realtasks folder of the current test project and change the file name to: <name of your test Job>. DLL. config, so that you can read the web when the unit test is run. config content
Question 2:
XML files are used in the project to read some files or other external resources, which cannot be loaded in unit tests.
Solution:
1. If appdomian. currentdomain. basedirectory is used in the code, put the configuration file in the bin \ debug or bin \ release folder.
2. you can enable test setting, check enable deployment on the tab, and select the XML file to be copied or other external resources. Note, in this way, the default related path is changed to the test case in the testresult file.
3. You can write scripts in test setting to copy files.
Question 3:
Some files may be loaded incorrectly after the Web cache function is used.
Solution:
1. cache uses absolute paths instead of relative paths in unit test.
To sum up, unit test will not make any changes every time after the WEB Project is modified (the function is not changed). My approach is to write a prework method in unit test, and initialize this method in a class, and all the unit test classes inherit from this class.
If you have any questions about unit test, contact me :)