Skip to content

[Development] Unit tests

Chen edited this page Jul 26, 2019 · 2 revisions

Setting up test cases

Before you can run most of the test cases, please create a new file named credentials.cs and place it under \UnitTestProject1\_private folder. The content should be like this

using System;
using WikiClientLibrary;
using WikiClientLibrary.Sites;

namespace UnitTestProject1
{
    partial class CredentialManager
    {
        static partial void Initialize()
        {
            // A place to perform page moving and deleting
            // You should have the bot or sysop right there
            DirtyTestsEntryPointUrl = "https://test2.wikipedia.org/w/api.php";
            // A private wiki API endpoint, where anonymous
            // users have no read permission.
            PrivateWikiTestsEntryPointUrl = null;
            LoginCoreAsyncHandler = async site =>
            {
                var url = site.ApiEndpoint;
                // See Endpoints.cs for a complete list of MediaWiki sites we are accessing.
                // We'll make changes to test2.wikipedia.org
                if (Regex.IsMatch(url, @"(wikipedia|wikidata|beta\.wmflabs)\.org", RegexOptions.IgnoreCase))
                    await site.LoginAsync("Alice", "password");
                // We'll make changes to MediaWiki 119 test Wiki
                else if (Regex.IsMatch(url, @"wikia\.com|wikia\.org|fandom\.com", RegexOptions.IgnoreCase))
                    await site.LoginAsync("Bob", "password");
                // Add other login routines if you need to.
                else if (url.Contains("domain.com"))
                    await site.LoginAsync("Calla", "password");
                else
                    throw new NotSupportedException();
            };
            EarlyLoginCoreAsyncHandler = async (client, options) =>
            {
                var url = options.ApiEndpoint;
                WikiSite site = null;

                // We are testing login to wikipedia.org via WikiSite constructor.
                // Place sensitive information here.
                if (url.Contains("wikipedia.org"))
                    site = new WikiSite(client, options, "Alice", "password");

                if (site != null) await site.Initialization;
                return site;
            };
        }
    }
}

You need to put valid user names and passwords into this file. As for the special API entry point URLs, they are optional, but certain tests will not work if you neglect or set them to null. As is configured in .gitignore, this file WILL NOT be included in the repository.