1、Test Your ASP.NET Apps with VSTS Web Tests and Load Tests,Benjamin Day http:/ http:/,About the speaker,Owner, Benjamin Day Consulting, Inc. Email: Web: http:/ Blog: http:/ Trainer Visual Studio Team System, Team Foundation Server Microsoft MVP for VSTS Microsoft VSTS/TFS Customer Advisory Council M
2、icrosoft Cloud Services Advisory Group Leader of Beantown.NET INETA User Group,What is a Web Test?,Unit test for exercising and validating an ASP.NET application Collection of web requests Typically represents a scenario through the web app Probably not a great candidate for Test-First Development V
3、alidation Rules to check the returned web page Extraction Rules extract values from a returned page for use in other parts of the test Recorded or coded Can be data-driven Building block for Load Tests,Demo: Create a basic WebTest,What can you add to a web test?,ASP.NET UI Requests Requests to a web
4、 page WebService Requests Microsoft says “Request to a web service” In reality, more like a chunk of text passed to an HTTP address Comments Transactions A subset of the web tests requests grouped together by name Used to get timing information portions of the test Calls to other web tests New for V
5、S2008,What can you add to a request?,HTTP Header Add a header and a value URL Query String Parameter POST Parameter File Upload Parameter Dependent Request(s) Request embedded into the page like an image () request By default, dependent requests are parsed and issued at runtime according to the Resp
6、onse Adding a dependent request forces a certain request to be made,Parameterization,Use context values to drive your requests and validations Minimizes hard coding Web server addresses for requests can be parameterized Parameterized values are stored in the test context Reference the values using d
7、ouble “squiggle bracket” syntax variable_name Allows you to pass in values from load tests Essential for data-driven web tests,Data-Driven Web Tests,Use an external data source to run your web test OLE DB source CSV XML Helps simulate multiple users Enables much more thorough tests Easy to do more c
8、omplex tests,Code Demo,Create a basic data-driven web test,Data-Driven Test Gotchas / Fixes,Sources are challenging to modify File-based (CSV, XML) use hard-coded paths Database connection string can only be replaced not modified Problem: what if your devs dont all have the same working directories?
9、 Problem: paths different during automated builds Problem: database might be different during automated builds Fixes: Reference file-based sources through a network share localhostWebTestDatasources Option: Adjust your “hosts” file C:WindowsSystem32driversetchosts Reference databases using “Trusted
10、Connection=true”,Data-Driven Web Test Best Practices,You should always be able to use version control to re-create a point-in-time version of your app This includes your unit tests If your tests are data-driven then the data source needs to be version controlled, too! You should be able to compile y
11、our app and run ALL the unit tests from an automatic build (continuous integration),How to get test data without much effort,Create a “DB Pro” project Visual Studio Team System 2008 Database Edition defines the structure of the database Option #1: Use a Test Data Generation Plan Option #1a: Put the
12、test data into a database and script it to a CSV Option #1b: Reference the test data directly in the database Option #2: Use “Post Deploy” Scripts to populate the database Database is your data source Compile / Deploy the database You can populate test data tables the database using “Post-Deployment
13、 Scripts” as part of the database “build”,Test Data Regular Expressions,Names: A-Zaeioua-z* Email Addresses: a-z0-9*a-z0-9*.(com|org|net|co.uk) Phone Numbers: (0-93)-0-93-0-94,Code Demo,Generate test data with DBPro Use SQL Server Management Studio to export to CSV Put CSV on a share Modify data sou
14、rce to reference the share,Web Test Properties,Stop On Error If true, the test stops running at the first error Default value is false User Name, Password Credentials for the request Basic auth or Windows Integrated auth Description Proxy Address of a proxy server for this request PreAuthenticate Se
15、nd the authentication in the header True, False,Request Properties,Url Url for the request Follow Redirects Method GET or POST Parse Dependent Requests Look for things like tags, style sheets requests, and download them Encoding Character format for the request Default is UTF-8,More Request Properti
16、es,Cache Control True = use locally cached version (IEs “Automatic” setting) False = Always download Think Time (seconds) Emulate a human reading/consuming the web page Timeout (seconds) How long before this request should fail? Record Results Record performance counter data when run from a load tes
17、t,Still More Request Properties,Version Version of HTTP to send: 1.0 or 1.1 Expected HTTP Status Code Default value = 0 200, 300 = OK 400, 500 = Error Expected Response URL,Validation & Extraction Rules,Rules,Validation Rules Check values in the response Can be “Global” to the web test Per request E
18、xtraction Rules Pull values from the response Put them into the Test Context for use in other requests,“Out of the box” Validation Rules,Form Field Validate a field for an expected value Field = Find Text Find a string anywhere in the Response Maximum Request Time Execution time in milliseconds Requ
19、ired Tag Html tag exists n times in the response Required Attribute Value Html tag exists with the expected attributes and values Response URL URL (after redirects) is same as during the recorded test,“Out of the box” Extraction Rules,Pull values from the response Test Context Extract Attribute Valu
20、e Extract Form Field Extract HTTP Header Gets the value for an HTTP Header Extract Regular Expression Use a regex to pull a value from the response Extract Text Using StartsWith and EndsWith Extract Hidden Fields,“Out of the box” rules are underwhelming,Not a bad starting point Missing some function
21、ality Example: cant validate/extract Label values A little confusing (maybe Im not that smart.)Good news: you can easily create your own,The Html Agility Pack,Free .NET Library to help parse/consume HTML http:/ Why do you need it? HTML is hard to parse Usually isnt valid XML (unclosed tags, case sen
22、sitivity problems) Simplifies custom web test rule development Add a reference to HtmlAgilityPack.dll from your custom rule assembly,HtmlAgilityPack Basics,HtmlAgilityPack namespace HtmlDocument Primary point of contact for working with the HTML Load() & LoadHtml() methods GetElementById() Fetches a
23、n HtmlNode (aka. html tag) by its “id” attribute value HtmlNode An element (aka. html tag) in the html InnerText property Retrieve the contents of the tag with any xml markup stripped out InnerHtml property Retrieve the raw XML/HTML contents of the tag,Create a New Web Test Validation Rule,Must be i
24、n a different DLL than the WebTests Microsoft.VisualStudio.QualityTools.WebTestFramework.dll C:Program FilesMicrosoft Visual Studio 9.0 Common7IDEPublicAssemblies Extend from ValidationRule Implement VS2005: RuleName, RuleDescription Properties Obsoleted in VS2008 VS2008: System.ComponentModel Displ
25、ayName(“name”), Description(“desc”) Method: Validate(),Custom Rules: ValidationEventArgs,Passed via the Validate() method Provides access to WebTestRequest WebTestResponse WebTest Set the error message via Message property Set pass/fail via IsValid property ValidationLevel,Code Demo,Ajax support Cre
26、ate a validation rule to validate an ASP.NET Label Create a validation rule to validate a ASP.NET GridView,Creating New Web Test Extraction Rule,Must be in a different DLL than the WebTests Microsoft.VisualStudio.QualityTools.WebTestFramework.dll C:Program FilesMicrosoft Visual Studio 9.0 Common7IDE
27、PublicAssemblies Extend from ExtractionRule Abstract class Implement VS2005: RuleName, RuleDescription Properties Obsoleted in VS2008 VS2008: System.ComponentModel DisplayName(“name”), Description(“desc”) Method: Extract(),Custom Rules: ExtractionEventArgs,Passed via the Extract() method Provides ac
28、cess to WebTestRequest WebTestResponse WebTest Success property (bool) Extract value(s) get set into the WebTestContext object via WebTest.Context.Add(string, string) method,Code Demo,Create an extraction rule to get an ASP.NET Labels value,WebTest Hassles,Data source management VS2005 version doesn
29、t do AJAX VS2008 does AJAX Doesnt run JavaScript Difficult to use “Cassini” Cassini = ASP.NET Development Web Server in Visual Studio Difficult to get the disk path to your web app difficult to start Cassini Best practice: use IIS App.config does not get loaded for non-coded web tests OUCH! Large-sc
30、ale html layout changes break tests,PLUGINS,Web Test Plugins,Allow you to hook into the Pre- and Post- web test execution events Similar to ClassInitialize & ClassCleanup Extend from WebTestPlugin class in Microsoft.VisualStudio.TestTools.WebTesting namespace of Microsoft.VisualStudio.QualityTools.W
31、ebTestFramework.dll Override PreWebTest() or PostWebTest() Class cannot be in the same project as the web test Downside: Cant access app.config values in the normal way,Web Test Request Plugin,External class (cannot be in the same project as the web test) Allow you to hook into the Pre- and Post- ex
32、ecution events Similar to TestInitialize & TestCleanup Extend from WebTestRequestPlugin class in Microsoft.VisualStudio.TestTools.WebTesting namespace of Microsoft.VisualStudio.QualityTools.WebTestFramework.dll Override PreRequest() and PostRequest() Downside: Cant access app.config values in the no
33、rmal way,Workaround for the app.config problem, Part 1,Load the app.config file ourselves and parse it http:/ PostID=122048 protected string GetConfigSetting(string key) tryExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();Assembly assembly = Assembly.GetExecutingAssembly();strin
34、g configFileName = new FileInfo(assembly.Location ).Name.Replace(“.dll“, “) + “.config“;configFileMap.ExeConfigFilename = configFileName; ,Workaround for the app.config problem, Part 2,. Configuration configObj = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.
35、None);string val = configObj.ConnectionStrings.ConnectionStrings “conn_string_name“.ConnectionString;return val; ,Load Testing,Create a load test by choosing existing unit tests Unit Tests Web Tests Stress tests the application by running the load test using simulated users Visual Studio Test Load A
36、gent Allows you to enlist other computers in the load test Approximately 1000 simulated users per processor (Disclaimer: actual results may vary) CDW.com Price on 11/14/2008 $4716.99,Demo: Create A Load Test,Team BUILD,Web Tests & Team Build,Automatically deploy your web app to IIS from MSBuild Adju
37、st references to custom plugins, rules from MSBuild Run the Web Tests without a Test List (.vsmdi) Deploy test data for data sourcesBug in Team Build 2008 Cant run web tests or load tests from team build This has been resolved in VS2008 SP1,Refresher: Structure of your Team Build,Team Build director
38、y Off of root of your source tree TFSBuild.proj The build script (MSBuild) TFSBuild.rsp “Command line” arguments fed to the team build Example: /verbosity:diag,Team Build: Default Targets,BeforeEndToEndIteration / AfterEndToEndIteration BuildNumberOverrideTarget BeforeClean / AfterClean BeforeGet /
39、AfterGet BeforeLabel / AfterLabel BeforeCompile / AfterCompile BeforeTest / AfterTest BeforeDropBuild / AfterDropBuild Before/after the outputs are copied to the drop directory BeforeOnBuildBreak / AfterOnBuildBreak Before/after the work item is created,Code Demo,Make it work in Team Build Configure
40、 IIS as part of the build Using MSBuild Community Tasks http:/msbuildtasks.tigris.org/,Summary,Created web tests Data-driven web tests Web test plugins Validation Rules, Extraction Rules with HtmlAgilityPack Load Tests Team Build,About the speaker,Owner, Benjamin Day Consulting, Inc. Email: Web: http:/ Blog: http:/ Trainer Visual Studio Team System, Team Foundation Server Microsoft MVP for VSTS Microsoft VSTS/TFS Customer Advisory Council Microsoft Cloud Services Advisory Group Leader of Beantown.NET INETA User Group,
copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1